2013-04-01 63 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]; 
    } 
}