How to save file in the documents folder?

≯℡__Kan透↙ 提交于 2019-12-17 16:42:06

问题


I am trying to save data in the documents folder on iPhone 5.1 simulator.

 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");
}

I've got always "Not Writable". Any idea? Please help me.


回答1:


Maybe because you have not created the file, the file you tested does not exist. :)

You can do this to find the problem,

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");
}



回答2:


Try this:

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];



回答3:


Swap out your [NSString stringWithFormat:] for an [NSString stringByAppendingPathComponent:]. This makes or breaks your ability to create a viable path, or so my experience has taught me.

Additionally, this sort of thing comes up when writing on the simulator vs a real device. The simulator is a lot more forgiving when you are saving things in the wrong paths than the device will be, and quite often you'll get the I/O working fine only to run into the notorious 'It works in the simulator!' pit. stringWithFormat: is just one way to fall in.




回答4:


get document directory path

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

write data to end of file

+(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];
}


来源:https://stackoverflow.com/questions/11053842/how-to-save-file-in-the-documents-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!