2012-06-15 81 views
3

我正在尝试将数据保存在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"); 
} 

我总是“不可写”。 有什么想法?请帮帮我。

+0

结束你有bu的json文件吗? ndle或动态创建。 –

+0

用于在路径中写入文件u需要将您的json文件创建为nsdata * data或者需要nsstring并使用[data writeAtPath:filePath atomically:YES]; –

回答

17

也许是因为您还没有创建该文件,那么您测试的文件不存在。 :)

你可以做到这一点,以发现问题,

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:只是一个坠入方式

+0

感谢您的建议。 – ttotto

5

试试这个:

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

写入数据

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

感谢您的建议。 – ttotto

0

获取文件目录路径文件

+(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]; 
} 
相关问题