如何从文档目录中读取文件

时间:2010-10-27 22:25:23

标签: file search ios-simulator

我要从文档目录中读取名为myname.plist的文件.....我想将文件的内容存储到字符串....

1 个答案:

答案 0 :(得分:3)

这样的事情应该做:

/* Find the documents directory. */
NSArray *paths = NSSearchPathForDirectoriesInDomains(
        NSDocumentsDirectory, NSUserDomainMask, YES/*expand tilde*/);
NSAssert([paths count] > 0,
         @"%s: No user document directory found!", __func__);
NSString *documentsDir = [paths objectAtIndex:0U];

/* Build the file path. */
NSString *name = [@"myname" stringByAppendingPathExtension:"plist"];
NSString *path = [documentsDir stringByAppendingPathComponent:name];

/* Read in the file content. */
NSError *error = nil;
NSStringEncoding enc;
NSString *content = [NSString
                     stringWithContentsOfFile:path
                     usedEncoding:&enc
                     error:&error];
if (!content) {
    NSLog(@"%s: Error reading content of \"%@\": %@", __func__, path, error);
}