object-c获取文件or路径

1,获取资源文件路径
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MyFile" ofType:@"txt"];
NSData *myData = [NSData dataWithContentsOfFile:filePath];
if (myData) {
	// do something useful
}
2,UIWebview加载本地htm文件
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"HelpDoc" ofType:@"htm"];
NSData *htmlData = [NSData dataWithContentsOfFile:filePath];
if (htmlData) {
	[webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@"http://iphoneincubator.com"]];
}
3,将资源文件的内容放到一个NSString对象中
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"important" ofType:@"txt"];
if (filePath) {
	NSString *myText = [NSString stringWithContentsOfFile:filePath];
	if (myText) {
		textView.text= myText;
	}
}

4,获取document路径

- (NSString *)getDocumentsDirectory {
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
/*
 NSString *storePath = [documentsDirectory stringByAppendingPathComponent:@"Data.txt"];
    NSURL *storeURL = [NSURL fileURLWithPath:storePath]
*/
	return [paths objectAtIndex:0];
}
原文地址:http://iphoneincubator.com/blog/data-management/how-to-read-a-file-from-your-application-bundle

你可能感兴趣的:(object-c获取文件or路径)