ios系统下删除文件的代码
方法一:这段objectivec代码用于删除指定路径的文件
if([fileManagerremoveItemAtPath:@"FilePath"error:NULL]){ NSLog(@"Removedsuccessfully"); }
方法二:
NSFileManager*defaultManager; defaultManager=[NSFileManagerdefaultManager]; [defaultManagerremoveFileAtPath:tildeFilename handler:nil];
handler可以接收消息,比如如果删除失败,可以使用fileManager:shouldProceedAfterError:。
方法三:
IOS删除文件删除文件夹创建文件创建文件夹判断文件存在md5封装类
自己最近在使用关于数据的存取和删除,于是自己就写了一个包括功能的类,自己用着还是蛮方便,再次分享一下
StorageData.m
// //StorageData.m //xunYi7 // //Createdbydavidon13-6-28. //Copyright(c)2013年david.Allrightsreserved. // #import<CommonCrypto/CommonDigest.h> #import"StorageData.h" #import"xunYi7AppDelegate.h" @implementationStorageData -(void)connection:(NSURLConnection*)connectiondidReceiveData:(NSData*)data{ NSLog(@"开始结didReceiveData搜数据"); } -(void)connection:(NSURLConnection*)connectiondidReceiveResponse:(NSURLResponse*)response{ NSLog(@"开始结didReceiveResponse搜数据"); } -(void)connection:(NSURLConnection*)connectiondidFailWithError:(NSError*)error{ NSLog(@"didFailWithError"); } -(void)connectionDidFinishLoading:(NSURLConnection*)connection{ NSLog(@"connectionDidFinishLoading"); } +(NSMutableData*)remoteFetchData:(NSString*)dataUrl{ NSString*currentDataFilePath=[[selfdataPath]stringByAppendingPathComponent:[selffetchTodayDate]]; //创建目录 currentDataFilePath=[selfcreateDirectory:currentDataFilePath]; currentDataFilePath=[currentDataFilePathstringByAppendingPathComponent:[NSStringstringWithFormat:@"%@.plist",[selfmd5:dataUrl]]]; if([xunYi7AppDelegateisReachable]){ NSURL*url=[[NSURLalloc]initWithString:dataUrl]; NSURLRequest*request=[[NSURLRequestalloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60]; NSURLResponse*response=[[NSURLResponsealloc]init]; NSError*receiveDataError=[[NSErroralloc]init]; NSMutableData*receivedData=(NSMutableData*)[NSURLConnectionsendSynchronousRequest:request returningResponse:&response error:&receiveDataError]; [UIApplicationsharedApplication].networkActivityIndicatorVisible=NO; returnreceivedData; }else{ [xunYi7AppDelegateshowNetworkMessage]; } returnnil; } +(NSMutableData*)localFetchData:(NSString*)dataUrl{ NSString*currentDataFilePath=[[selfdataPath]stringByAppendingPathComponent:[selffetchTodayDate]]; NSString*yesterdayDataFilePath=[[selfdataPath]stringByAppendingPathComponent:[selffetchYesterdayDate]]; //创建目录 currentDataFilePath=[selfcreateDirectory:currentDataFilePath]; currentDataFilePath=[currentDataFilePathstringByAppendingPathComponent:[NSStringstringWithFormat:@"%@.plist",[selfmd5:dataUrl]]]; yesterdayDataFilePath=[yesterdayDataFilePathstringByAppendingPathComponent:[NSStringstringWithFormat:@"%@.plist",[selfmd5:dataUrl]]]; NSMutableData*localData=[selffromFilenamePathFetchLocalData:currentDataFilePath]; if(localData!=nil){//本地数据 returnlocalData; }else{//远程获取数据 NSMutableData*receivedData=[selfremoteFetchData:dataUrl]; if(receivedData!=nil){ if([selfstorageDataToFile:receivedDatafileName:currentDataFilePath]){ NSLog(@"保存成功"); [selfremoveDirectory]; }else{ NSLog(@"保存失败"); } }else{ if((localData=[selffromFilenamePathFetchLocalData:yesterdayDataFilePath])!=nil){ returnlocalData; } } returnreceivedData; } returnnil; } //md5加密字符串 +(NSString*)md5:(NSString*)str{ constchar*cStr=[strUTF8String]; unsignedcharresult[16]; CC_MD5(cStr,strlen(cStr),result);//Thisisthemd5call return[NSStringstringWithFormat: @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", result[0],result[1],result[2],result[3], result[4],result[5],result[6],result[7], result[8],result[9],result[10],result[11], result[12],result[13],result[14],result[15] ]; } //上传图片存储 +(void)saveUploadImage:(UIImage*)imagewithName:(NSString*)imageName{ NSFileManager*fileManager=[[NSFileManageralloc]init]; NSError*error; //获取沙盒目录 NSString*fullPath=[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]; fullPath=[fullPathstringByAppendingPathComponent:@"tmpImage"]; if(![fileManagerfileExistsAtPath:fullPath]){ [fileManagercreateDirectoryAtPath:fullPath withIntermediateDirectories:YES attributes:nil error:&error]; } fullPath=[fullPathstringByAppendingPathComponent:imageName]; NSData*imageData=UIImageJPEGRepresentation(image,0.5); //将图片写入文件 [imageDatawriteToFile:fullPathatomically:NO]; } //上传图片删除 +(void)removeUploadImage:(UIImage*)imagewithName:(NSString*)imageName{ NSFileManager*fileManager=[[NSFileManageralloc]init]; NSError*error; //获取沙盒目录 NSString*fullPath=[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]; fullPath=[fullPathstringByAppendingPathComponent:@"tmpImage"]; if(![fileManagerfileExistsAtPath:fullPath]){ [fileManagerremoveItemAtPath:fullPatherror:&error]; } } //获取存储的图片 +(NSString*)fetchUploadImagePath:(NSString*)imageName{ NSString*fullPath=[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]; fullPath=[fullPathstringByAppendingPathComponent:@"tmpImage"]; fullPath=[fullPathstringByAppendingPathComponent:imageName]; returnfullPath; } //判断文件是否存在 +(NSString*)isFileExists:(NSString*)fullpath{ NSFileManager*fileManager=[[NSFileManageralloc]init]; if([fileManagerfileExistsAtPath:fullpath]){ returnfullpath; } returnnil; } //数据存储 //+(void) //获取存储文件的目录 +(NSString*)dataPath{ //此处首先指定了图片存取路径(默认写到应用程序沙盒中) NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); //并给文件起个文件名 NSString*filePathDerectory=[pathsobjectAtIndex:0]; returnfilePathDerectory; } //获取指定文件的数据 +(NSMutableData*)fromFilenamePathFetchLocalData:(NSString*)filename{ //保存数据到指定文件中 NSFileManager*fileManager=[[NSFileManageralloc]init]; if([fileManagerfileExistsAtPath:filename]){ NSData*data=[fileManagercontentsAtPath:filename]; return[datamutableCopy]; } returnnil; } //存储数据到指定文件 +(BOOL)storageDataToFile:(NSData*)datafileName:(NSString*)fileName{ //保存数据到指定文件中 NSFileManager*fileManager=[[NSFileManageralloc]init]; if([fileManagercreateFileAtPath:fileNamecontents:dataattributes:nil]){ returnYES; }else{ returnNO; } } //删除文件 +(void)deleteFile:(NSString*)fileName{ NSFileManager*fileManager=[[NSFileManageralloc]init]; NSError*error; [fileManagerremoveItemAtPath:fileNameerror:&error]; } //获取今天的日期 +(NSString*)fetchTodayDate{ NSDate*currentDate=[NSDatedate]; NSDateFormatter*dateFormatter=[[NSDateFormatteralloc]init]; [dateFormattersetDateStyle:NSDateFormatterMediumStyle]; return[dateFormatterstringFromDate:currentDate]; } //获取昨天的日期 +(NSString*)fetchYesterdayDate{ NSDate*yesterdayDate=[NSDatedateWithTimeIntervalSinceNow:-(24*60*60)]; NSDateFormatter*dateFormatter=[[NSDateFormatteralloc]init]; [dateFormattersetDateStyle:NSDateFormatterMediumStyle]; return[dateFormatterstringFromDate:yesterdayDate]; } //获取前天的日期 +(NSString*)fetchYesterdayBeforeDate{ NSDate*yesterdayDate=[NSDatedateWithTimeIntervalSinceNow:-(2*(24*60*60))]; NSDateFormatter*dateFormatter=[[NSDateFormatteralloc]init]; [dateFormattersetDateStyle:NSDateFormatterMediumStyle]; return[dateFormatterstringFromDate:yesterdayDate]; } //获取存储文件的数据 //创建文件 //创建目录 +(NSString*)createDirectory:(NSString*)directoryName{ NSFileManager*fileManager=[[NSFileManageralloc]init]; NSError*error; if(![fileManagerfileExistsAtPath:directoryName]){ [fileManagercreateDirectoryAtPath:directoryName withIntermediateDirectories:YES attributes:nil error:&error]; if(error==nil){ returndirectoryName; }else{ returndirectoryName; } }else{ returndirectoryName; } } //删除文件 +(void)removeFile:(NSString*)filePath{ NSError*error; NSFileManager*fileManager=[[NSFileManageralloc]init]; if([fileManagerfileExistsAtPath:filePath]){ [fileManagerremoveItemAtPath:filePatherror:&error]; } if(error){ NSLog(@"error=%@",error); } } //删除目录 +(void)removeDirectory{ NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString*documentsPath=[pathsobjectAtIndex:0]; NSString*removeDirectoryPath=[documentsPathstringByAppendingPathComponent:[selffetchYesterdayBeforeDate]]; NSError*error; NSFileManager*fileManager=[[NSFileManageralloc]init]; if([fileManagerfileExistsAtPath:removeDirectoryPath]){ [fileManagerremoveItemAtPath:removeDirectoryPatherror:&error]; } if(error){ NSLog(@"error=%@",error); } } @end StorageData.h // //StorageData.h //xunYi7 // //Createdbydavidon13-6-28. //Copyright(c)2013年david.Allrightsreserved. // #import<Foundation/Foundation.h> @interfaceStorageData:NSObject<NSURLConnectionDataDelegate,NSURLConnectionDelegate> +(NSMutableData*)remoteFetchData:(NSString*)dataUrl; +(NSMutableData*)localFetchData:(NSString*)dataUrl; +(void)saveUploadImage:(UIImage*)imagewithName:(NSString*)imageName; +(NSString*)uploadImage:(UIImage*)imagewithName:(NSString*)imageName; +(NSString*)fetchUploadImagePath; +(NSString*)fetchUploadImagePath:(NSString*)imageName; +(void)removeUploadImage:(UIImage*)imagewithName:(NSString*)imageName; +(NSString*)isFileExists:(NSString*)fullpath; +(void)removeFile:(NSString*)filePath; @end
有不完善的地方,希望指正和修改