无法创建用于编写的文件 - iOS

时间:2013-04-01 21:43:21

标签: ios serialization nsfilemanager

所以我编写了一堆用于写入的对象,但是我在文件管理方面遇到了麻烦。我似乎无法打开现有文件,或创建新文件。

我尝试过创建一个同名的空文件,或者只是创建一个新文件。它似乎没有回应(或者根本不做任何事情)。

编辑所以我在这里遵循了一些建议,现在我的访问崩溃了。

这是我得到路径的地方:

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

3 个答案:

答案 0 :(得分:4)

您无权写入root用户,只能访问沙盒,您正在创建只读句柄。试试这个:

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

答案 1 :(得分: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];

答案 2 :(得分: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];
   }
}