【问题标题】:how to avoid overwrite file如何避免覆盖文件
【发布时间】:2011-03-03 11:14:17
【问题描述】:

我正在使用 NSURLConnection 从服务器下载文件并将其存储在本地,如下所示

-(void) connection:(NSURLConnection *)connection 
    didReceiveData:(NSData *)data  
{  
   [webData appendData:data];  
   paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);  

   self.documentsDir = [[paths objectAtIndex:0]stringByAppendingPathComponent:@"NewResult.zip" ];  

   [[NSFileManager defaultManager] createFileAtPath:documentsDir contents:nil attributes:nil];  
   NSFileHandle *file1 = [NSFileHandle fileHandleForUpdatingAtPath: documentsDir];  
   [file1 writeData: webData];  

   [file1 closeFile];  
}  

这是有效的,但每次都会覆盖旧文件 如何避免这种过度写入我知道使用 NSURLDownload 可以做到这一点,但这里我没有使用 NSURLDownload。

【问题讨论】:

    标签: iphone objective-c file nsurlconnection


    【解决方案1】:
    if (![fileManager fileExistsAtPath: path]){
        NSLog(@"File not exists at path %@", path);
        }else{
        NSLog(@"File exists at path:%@", path);
    }
    

    【讨论】:

    • 这只会显示文件是否已经存在,但我想每次都创建新文件,如 NewResult1、NewResult2、NewResult3、...等等
    • 获取计数器并创建自己的文件路径
    猜你喜欢
    • 2019-05-30
    • 1970-01-01
    • 2014-06-06
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 2015-05-15
    • 2015-07-10
    相关资源
    最近更新 更多