简体   繁体   English

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

[英]how to read a file from documents directory

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

Something like this should do: 这样的事情应该做:

/* 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM