简体   繁体   中英

cannot create a file for writing - iOS

So I have encoded a bunch of objects for writing, but I'm having trouble with the file management. I can't seem to open the existing file, or create a new file.

I've tried creating an empty file of the same name, or just creating a new file. It doesn't seem to respond (or do anything at all for that matter).

edit So I've followed some of the advice here, and now I'm getting a bad access crash.

Here's where I get the path:

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

And here's the modified method.

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

You don't have permissions to write to root, you can only access your sandbox, and you are creating a read-only handle. Try this way:

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

You are trying to open a file at the root of the filesystem, but you don't have access to that on iOS. Create the file in the documents directory instead:

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

You need to write the file at the documents directory. And then use stringByAppendingPathComponent tu create the full path. This should work, I hope this helps...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM