如何避免覆盖文件

时间:2011-03-03 11:14:17

标签: iphone objective-c file nsurlconnection

我正在使用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。

1 个答案:

答案 0 :(得分:3)

if (![fileManager fileExistsAtPath: path]){
    NSLog(@"File not exists at path %@", path);
    }else{
    NSLog(@"File exists at path:%@", path);
}