【问题标题】:how to read a file from documents directory如何从文档目录中读取文件
【发布时间】:2010-10-27 22:25:23
【问题描述】:

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

【问题讨论】:

    标签: file search ios-simulator


    【解决方案1】:

    应该这样做:

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

    【讨论】:

      猜你喜欢
      • 2012-09-24
      • 2014-06-07
      • 2015-09-19
      • 1970-01-01
      • 1970-01-01
      • 2011-09-26
      • 2011-10-09
      • 2013-02-03
      • 2013-10-25
      相关资源
      最近更新 更多