2012-06-15 203 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]; 
} 
相關問題