Read/write file in Documents directory problem

做~自己de王妃 提交于 2019-11-29 11:04:59

Instead of using NSFileManager to get the contents of that file, try using NSString as such:

NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
NSString *documentsDirectory = [pathArray objectAtIndex:0];
NSString *textPath = [documentsDirectory stringByAppendingPathComponent:@"file1.txt"];
NSError *error = nil;
NSString *str = [NSString stringWithContentsOfFile:textPath encoding:NSUTF8StringEncoding error:&error];
if (error != nil) {
    NSLog(@"There was an error: %@", [error description]);
} else {
    NSLog(@"Text file data: %@", str);
}

Edit: Added error checking code.

How are you getting documentsDirectory?

You should be using something like this:

NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)

NSString *documentsDirectory = [pathArray lastObject];

Put an NSLog statement after every line where you are setting a variable's value, so that you can inspect those values. This should help you quickly pinpoint where things start to go wrong.

The problem got solved by setting a non standard Bundle ID in die info.plist

I used the Bundle ID from iTunes Connect for this specific app. Now everything works perfectly.

You can also use NSFileHandle for writing data in file and save to document directory:

Create a variable of NSFileHandle

NSFileHandle *outputFileHandle;

use the prepareDataWrittenHandle function with passing file name in parameter with file extension

-(void)prepareDataWrittenHandle:(NSString *)filename
{
    //Create Path of file in document directory.
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *outputFilePath = [documentsDirectory stringByAppendingPathComponent:filename];

    //Check file with above name is already exits or  not.
    if ([[NSFileManager defaultManager] fileExistsAtPath:outputFilePath] == NO) {
        NSLog(@"Create the new file at outputFilePath: %@", outputFilePath);

        //Create file at path.
        BOOL suc = [[NSFileManager defaultManager] createFileAtPath:outputFilePath
                                                           contents:nil
                                                         attributes:nil];
        NSLog(@"Create file successful?: %u", suc);
    }

    outputFileHandle = [NSFileHandle fileHandleForWritingAtPath:outputFilePath];
}

then write the string value to file as:

//Create a file
[self prepareDataWrittenHandle:@"file1.txt"];

//String to save in file
NSString *text = @"My cool text message";

//Convert NSString to NSData to save data in file.
NSData* data = [text dataUsingEncoding:NSUTF8StringEncoding]

//Write NSData to file
[_outputFileHandle writeData:data];

//close the file if written complete
[_outputFileHandle closeFile];

at the end of file written you should close the file.

You can also check the content written in file as NSString for debug point of view as mention above by @Glenn Smith:

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *outputFilePath = [documentsDirectory stringByAppendingPathComponent:@"file1.txt"];

NSError *error;
NSString *str = [NSString stringWithContentsOfFile:outputFilePath encoding:NSUTF8StringEncoding error:&error];
if (error != nil) {
    NSLog(@"There was an error: %@", [error description]);
} else {
    NSLog(@"Text file data: %@", str);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!