2013-04-01 65 views
0

因此,我編寫了一堆用於寫入的對象,但我遇到了文件管理問題。我似乎無法打開現有的文件,或創建一個新的文件。無法創建寫入文件 - iOS

我試着創建一個同名的空文件,或者只是創建一個新文件。它似乎沒有迴應(或者就此事做任何事情)。

編輯所以我在這裏遵循了一些建議,現在我得到了一個不好的訪問崩潰。

這裏就是我得到的路徑:

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    filePath = [documentsDirectory stringByAppendingPathComponent:@"/bucketListData.dat"]; 

而這裏的修改方法。

-(void) writeFile 
{ 
    NSData *encodedObject; 
    encodedObject = [NSKeyedArchiver archivedDataWithRootObject: bucketItems]; 
    NSFileHandle *file; 
    file = [NSFileHandle fileHandleForReadingAtPath:filePath]; 

    if(file == nil) 
    { 
     NSLog(@"Failed to open a file handle for writing. Attempting to create a file."); 
     [[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil]; 
     file = [NSFileHandle fileHandleForReadingAtPath:filePath]; 
     if(file == nil) 
     { 
      NSLog(@"Unable to create new file."); 
     } 
    } 

    [file writeData: encodedObject]; 
    [file closeFile]; 
} 
+0

只有使用[tag:objective-c]標記的問題具體關於語言本身。謝謝! – Undo

+0

編輯從原來表示更改 –

回答

4

您沒有權限寫入根,你只能訪問沙箱,要創建一個只讀處理。試試這種方式:

NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
NSString *filePath = [docDir stringByAppendingPathComponent:@"bucketListData.dat"]; 
NSFileHandle *file = [NSFileHandle fileHandleForWritingAtPath:filePath]; 

if (!file) { 
    NSLog(@"Attempting to create the file"); 
    if (![[NSFileManager defaultManager] createFileAtPath:filePath 
               contents:nil 
               attributes:nil]) { 
     NSLog(@"Failed to create file"); 
    } 
    else { 
     file = [NSFileHandle fileHandleForWritingAtPath:filePath]; 
    } 
} 

NSLog(@"File handle: %@", file); 
+0

,似乎並沒有完成的技巧..調用createFileAtPath後,文件仍然爲空 –

+0

@ScubaSteve添加更多的代碼在新項目中爲我工作,再試一次。 – Pascal

+0

IVE編輯上面所推薦的線我的代碼..它不工作 –

1

您試圖在文件系統的根目錄下打開文件,但您無權訪問iOS上的文件。在文檔目錄,而不是創建文件:

NSData* encodedObject = [NSKeyedArchiver archivedDataWithRootObject: bucketItems]; 

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString* basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; 
NSString* path = [basePath stringByAppendingPathComponent:@"bucketListData.dat"]; 
NSFileHandle* file = [NSFileHandle fileHandleForReadingAtPath:path]; 
1

您需要將文件寫入文檔目錄。然後使用stringByAppendingPathComponent tu創建完整路徑。這應該工作,我希望這可以幫助...

-(void) writeFile 
{ 
    NSData *encodedObject; 
    encodedObject = [NSKeyedArchiver archivedDataWithRootObject: bucketItems]; 
    NSFileHandle *file; 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory; 

    if(paths && [paths count]>0){ 
     documentsDirectory=[paths objectAtIndex:0]; 

     file = [NSFileHandle fileHandleForReadingAtPath:[documentsDirectory stringByAppendingPathComponent:@"/bucketListData.dat"]]; 

     if(file == nil) 
     { 
      NSLog(@"Failed to open a file handle for writing. Attempting to create a file."); 
      [[NSFileManager defaultManager] createFileAtPath:[documentsDirectory stringByAppendingPathComponent:@"/bucketListData.dat"] contents:nil attributes:nil]; 
      file = [NSFileHandle fileHandleForReadingAtPath:[documentsDirectory stringByAppendingPathComponent:@"/bucketListData.dat"]]; 
      if(file == nil) 
      { 
       NSLog(@"Unable to create new file."); 
      } 
     } 

     [file writeData: encodedObject]; 
     [file closeFile]; 
    } 
}