【问题标题】:How to save file in the documents folder?如何将文件保存在文档文件夹中?
【发布时间】:2012-06-18 17:03:22
【问题描述】:

我正在尝试将数据保存在 iPhone 5.1 模拟器上的文档文件夹中。

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, @"myData.json"];

if ([[NSFileManager defaultManager] isWritableFileAtPath:filePath]) {
    NSLog(@"Writable");
}else {
    NSLog(@"Not Writable");
}

我总是“不可写”。 任何想法?请帮帮我。

【问题讨论】:

  • 你有 json 文件在 bundle 中还是动态创建的。
  • 要在路径写入文件,您需要将 json 文件创建到 nsdata *data 或您需要 nsstring 并使用 [data writeAtPath:filePath atomically:YES];

标签: iphone nsfilemanager nsdocumentdirectory


【解决方案1】:

获取文档目录路径

+(NSURL *)getDocumentsDirectoryPath
{
    return [[[NSFileManager defaultManager]URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]lastObject];
}

将数据写入文件末尾

+(void)saveText:(NSString *)textTobeSaved atPath:(NSString*)fileName
{
    NSString *filePath = [NSString stringWithFormat:@"%@.text",fileName];

    NSString *path = [[self getDocumentsDirectoryPath].path
                      stringByAppendingPathComponent:filePath];
    NSFileHandle *fileHandler = [NSFileHandle fileHandleForWritingAtPath:path];
    if(fileHandler == nil) {
        [[NSFileManager defaultManager] createFileAtPath:path contents:nil attributes:nil];
        fileHandler = [NSFileHandle fileHandleForWritingAtPath:path];
    } else {
        textTobeSaved = [NSString stringWithFormat:@"\n-----------------------\n %@",textTobeSaved];
        [fileHandler seekToEndOfFile];
    }

    [fileHandler writeData:[textTobeSaved dataUsingEncoding:NSUTF8StringEncoding]];
    [fileHandler closeFile];
}

【讨论】:

    【解决方案2】:

    可能因为你没有创建文件,你测试的文件不存在。 :)

    你可以这样做来找出问题,

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, @"myData.json"];
    NSLog(@"filePath %@", filePath);
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) { // if file is not exist, create it.
        NSString *helloStr = @"hello world";
        NSError *error;
        [helloStr writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
    }
    
    if ([[NSFileManager defaultManager] isWritableFileAtPath:filePath]) {
        NSLog(@"Writable");
    }else {
        NSLog(@"Not Writable");
    }
    

    【讨论】:

      【解决方案3】:

      将您的[NSString stringWithFormat:] 换成[NSString stringByAppendingPathComponent:]。这决定或破坏了您创建可行路径的能力,或者我的经验告诉了我。

      此外,在模拟器上与真实设备上编写时会出现这种情况。当您将内容保存在错误的路径中时,模拟器比设备更宽容,而且您通常会让 I/O 工作正常,只是遇到臭名昭著的“它在模拟器中工作!”坑。 stringWithFormat: 只是其中一种方式。

      【讨论】:

        【解决方案4】:

        试试这个:

        NSString *data = .... // your json representation
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"myData.json"];
        [data writeToFile:appFile atomically:YES encoding:NSUTF8StringEncoding error:nil];
        

        【讨论】:

          猜你喜欢
          • 2021-10-19
          • 1970-01-01
          • 1970-01-01
          • 2015-02-22
          • 2015-10-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多