NSFileHandle fileHandleForWritingAtPath:返回null!
我的iPad应用程序有一个小的下载工具,我想用NSFileHandle附加数据。 问题是创build调用只返回空文件句柄。 可能是什么问题呢? 这是应该创build我的文件句柄的三行代码:
NSString *applicationDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; self.finalPath = [applicationDocumentsDirectory stringByAppendingPathComponent: self.fileName]; NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath];
我检查了文件path,我没有看到任何错误。
TYIA
fileHandleForWritingAtPath
不是“创build”调用。 该文档明确指出:“返回值:初始化的文件句柄,或者如果path中没有文件存在,则为零 ”(强调添加)。 如果你想创build文件,如果它不存在,你必须使用这样的东西:
NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath]; if(output == nil) { [[NSFileManager defaultManager] createFileAtPath:self.finalPath contents:nil attributes:nil]; output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath]; }
如果你想附加到文件,如果它已经存在,使用类似[output seekToEndOfFile]
。 您的完整代码将如下所示:
NSString *applicationDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; self.finalPath = [applicationDocumentsDirectory stringByAppendingPathComponent: self.fileName]; NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath]; if(output == nil) { [[NSFileManager defaultManager] createFileAtPath:self.finalPath contents:nil attributes:nil]; output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath]; } else { [output seekToEndOfFile]; }
获取文档目录path
+(NSURL *)getDocumentsDirectoryPath { return [[[NSFileManager defaultManager]URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]lastObject]; }
将文本保存到文件结尾
如果文件不存在,创build它并写入数据
+(void)saveText:(NSString *)textTobeSaved atPath:(NSString*)fileName { NSString *filePath = [NSString stringWithFormat:@"%@.text",fileName]; NSString *path = [[self getDocumentsDirectoryPath].path stringByAppendingPathComponent:filePath]; NSFileHandle *fileHandler = [NSFileHandle fileHandleForWritingAtPath:path]; if(fileHandler == nil) { [[NSFileManager defaultManager] createFileAtPath:path contents:nil attributes:nil]; fileHandler = [NSFileHandle fileHandleForWritingAtPath:path]; } else { textTobeSaved = [NSString stringWithFormat:@"\n-----------------------\n %@",textTobeSaved]; [fileHandler seekToEndOfFile]; } [fileHandler writeData:[textTobeSaved dataUsingEncoding:NSUTF8StringEncoding]]; [fileHandler closeFile]; }
从指定文件名的文件中获取文本
+(NSString *)getTextFromFilePath:(NSString *)fileName { NSString *filePath = [NSString stringWithFormat:@"%@.text",fileName]; NSString *path = [[self getDocumentsDirectoryPath].path stringByAppendingPathComponent:filePath]; NSLog(@"%@",path); if(path!=nil) { NSString *savedString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; return savedString; }else{ return @""; } }
删除文件
+(void)deleteFile:(NSString *)fileName { NSString *filePath = [NSString stringWithFormat:@"%@.text",fileName]; NSString *path = [[self getDocumentsDirectoryPath].path stringByAppendingPathComponent:filePath]; NSFileHandle *fileHandler = [NSFileHandle fileHandleForWritingAtPath:path]; if(fileHandler != nil) { [[NSFileManager defaultManager]removeItemAtPath:path error:nil]; } }