首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSFileHandle fileHandleForWritingAtPath:返回null!

NSFileHandle fileHandleForWritingAtPath:返回null!
EN

Stack Overflow用户
提问于 2010-09-08 11:01:58
回答 2查看 13.6K关注 0票数 37

我的iPad应用程序有一个小的下载工具,我想要使用NSFileHandle附加数据。问题是创建调用只返回空文件句柄。可能的问题是什么?下面是创建我的文件句柄的三行代码:

代码语言:javascript
复制
NSString *applicationDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
self.finalPath = [applicationDocumentsDirectory stringByAppendingPathComponent: self.fileName]; 
NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath];

我检查了文件路径,没有发现任何问题。

TYIA

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-09-17 21:15:27

fileHandleForWritingAtPath不是一个“创建”调用。文档明确声明:“返回值:已初始化的文件句柄,如果路径处不存在文件,则返回nil”(强调添加)。如果您希望在文件不存在的情况下创建该文件,则必须使用以下内容:

代码语言:javascript
复制
 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]的东西。然后,您的完整代码将如下所示:

代码语言:javascript
复制
 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];
 }
票数 88
EN

Stack Overflow用户

发布于 2017-11-02 17:02:57

获取文档目录路径

代码语言:javascript
复制
+(NSURL *)getDocumentsDirectoryPath
{
    return [[[NSFileManager defaultManager]URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]lastObject];
}

将文本保存到文件的末尾

如果文件不存在,则创建它并写入数据

代码语言:javascript
复制
+(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];
}

从具有指定文件名的文件获取文本

代码语言:javascript
复制
+(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 @"";
  } 
}

删除文件

代码语言:javascript
复制
+(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];
    }
    
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3664362

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档