【问题标题】:NSFileHandle fileHandleForWritingAtPath: return null!NSFileHandle fileHandleForWritingAtPath:返回空!
【发布时间】:2010-09-08 03:01:57
【问题描述】:

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

NSString *applicationDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
self.finalPath = [applicationDocumentsDirectory stringByAppendingPathComponent: self.fileName]; 
NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath];

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

TYIA

【问题讨论】:

    标签: cocoa-touch nsfilehandle


    【解决方案1】:

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

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

    【讨论】:

    • 我的文件存在于“/var/mobile/Containers/Data/Application/E914726C-34B7-4B92-A740-90E31131D75E/Library/Caches/”,但我仍然为零。
    【解决方案2】:

    获取文档目录路径

    +(NSURL *)getDocumentsDirectoryPath
    {
        return [[[NSFileManager defaultManager]URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]lastObject];
    }
    

    将文本保存到文件末尾

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-23
      • 2021-07-07
      • 2013-09-30
      • 2021-08-23
      • 2013-04-07
      • 2018-05-21
      相关资源
      最近更新 更多