ios系统下删除文件的代码
程序员文章站
2022-06-09 19:43:47
方法一:这段objective c代码用于删除指定路径的文件
if ([filemanager removeitematpath:@"filepath" erro...
方法一:这段objective c代码用于删除指定路径的文件
if ([filemanager removeitematpath:@"filepath" error:null]) { nslog(@"removed successfully"); }
方法二:
nsfilemanager *defaultmanager; defaultmanager = [nsfilemanager defaultmanager]; [defaultmanager removefileatpath: tildefilename handler: nil];
handler可以接收消息,比如如果删除失败,可以使用filemanager:shouldproceedaftererror: 。
方法三:
ios 删除文件 删除文件夹 创建文件 创建文件夹 判断文件存在 md5 封装类
自己最近在使用关于数据的存取和删除,于是自己就写了一个包括功能的类,自己用着还是蛮方便,再次分享一下
storagedata.m
// // storagedata.m // xunyi7 // // created by david on 13-6-28. // copyright (c) 2013年 david. all rights reserved. // #import <commoncrypto/commondigest.h> #import "storagedata.h" #import "xunyi7appdelegate.h" @implementation storagedata -(void) connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data{ nslog(@"开始结didreceivedata搜数据"); } -(void) connection:(nsurlconnection *)connection didreceiveresponse:(nsurlresponse *)response{ nslog(@"开始结didreceiveresponse搜数据"); } -(void) connection:(nsurlconnection *)connection didfailwitherror:(nserror *)error{ nslog(@"didfailwitherror"); } -(void) connectiondidfinishloading:(nsurlconnection *)connection{ nslog(@"connectiondidfinishloading"); } +(nsmutabledata *)remotefetchdata:(nsstring *)dataurl{ nsstring *currentdatafilepath = [[self datapath] stringbyappendingpathcomponent:[self fetchtodaydate]]; //创建目录 currentdatafilepath = [self createdirectory:currentdatafilepath]; currentdatafilepath = [currentdatafilepath stringbyappendingpathcomponent:[nsstring stringwithformat:@"%@.plist",[self md5:dataurl]]]; if([xunyi7appdelegate isreachable]){ nsurl *url = [[nsurl alloc] initwithstring:dataurl]; nsurlrequest *request = [[nsurlrequest alloc] initwithurl:url cachepolicy:nsurlrequestuseprotocolcachepolicy timeoutinterval:60]; nsurlresponse *response = [[nsurlresponse alloc] init]; nserror *receivedataerror = [[nserror alloc] init]; nsmutabledata *receiveddata = (nsmutabledata *)[nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&receivedataerror]; [uiapplication sharedapplication].networkactivityindicatorvisible = no; return receiveddata; }else{ [xunyi7appdelegate shownetworkmessage]; } return nil; } +(nsmutabledata *)localfetchdata:(nsstring *)dataurl{ nsstring *currentdatafilepath = [[self datapath] stringbyappendingpathcomponent:[self fetchtodaydate]]; nsstring *yesterdaydatafilepath = [[self datapath] stringbyappendingpathcomponent:[self fetchyesterdaydate]]; //创建目录 currentdatafilepath = [self createdirectory:currentdatafilepath]; currentdatafilepath = [currentdatafilepath stringbyappendingpathcomponent:[nsstring stringwithformat:@"%@.plist",[self md5:dataurl]]]; yesterdaydatafilepath = [yesterdaydatafilepath stringbyappendingpathcomponent:[nsstring stringwithformat:@"%@.plist",[self md5:dataurl]]]; nsmutabledata *localdata = [self fromfilenamepathfetchlocaldata:currentdatafilepath]; if(localdata != nil){//本地数据 return localdata; }else{//远程获取数据 nsmutabledata *receiveddata = [self remotefetchdata:dataurl]; if(receiveddata != nil){ if([self storagedatatofile:receiveddata filename:currentdatafilepath]){ nslog(@"保存成功"); [self removedirectory]; }else{ nslog(@"保存失败"); } }else{ if((localdata = [self fromfilenamepathfetchlocaldata:yesterdaydatafilepath]) != nil){ return localdata; } } return receiveddata; } return nil; } //md5加密字符串 +(nsstring *)md5:(nsstring *)str{ const char *cstr = [str utf8string]; unsigned char result[16]; cc_md5(cstr, strlen(cstr), result); // this is the md5 call return [nsstring stringwithformat: @"%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 *)image withname:(nsstring *)imagename{ nsfilemanager *filemanager = [[nsfilemanager alloc] init]; nserror *error; // 获取沙盒目录 nsstring *fullpath = [nshomedirectory() stringbyappendingpathcomponent:@"documents"]; fullpath = [fullpath stringbyappendingpathcomponent:@"tmpimage"]; if(![filemanager fileexistsatpath:fullpath]){ [filemanager createdirectoryatpath:fullpath withintermediatedirectories:yes attributes:nil error:&error]; } fullpath = [fullpath stringbyappendingpathcomponent:imagename]; nsdata *imagedata = uiimagejpegrepresentation(image, 0.5); // 将图片写入文件 [imagedata writetofile:fullpath atomically:no]; } //上传图片删除 +(void) removeuploadimage:(uiimage *)image withname:(nsstring *)imagename{ nsfilemanager *filemanager = [[nsfilemanager alloc] init]; nserror *error; // 获取沙盒目录 nsstring *fullpath = [nshomedirectory() stringbyappendingpathcomponent:@"documents"]; fullpath = [fullpath stringbyappendingpathcomponent:@"tmpimage"]; if(![filemanager fileexistsatpath:fullpath]){ [filemanager removeitematpath:fullpath error:&error]; } } //获取存储的图片 +(nsstring *)fetchuploadimagepath:(nsstring *)imagename{ nsstring *fullpath = [nshomedirectory() stringbyappendingpathcomponent:@"documents"]; fullpath = [fullpath stringbyappendingpathcomponent:@"tmpimage"]; fullpath = [fullpath stringbyappendingpathcomponent:imagename]; return fullpath; } //判断文件是否存在 +(nsstring *)isfileexists:(nsstring *)fullpath{ nsfilemanager *filemanager = [[nsfilemanager alloc] init]; if([filemanager fileexistsatpath:fullpath]){ return fullpath; } return nil; } //数据存储 //+(void) //获取存储文件的目录 +(nsstring *)datapath{ //此处首先指定了图片存取路径(默认写到应用程序沙盒 中) nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory,nsuserdomainmask, yes); //并给文件起个文件名 nsstring *filepathderectory = [paths objectatindex:0]; return filepathderectory; } //获取指定文件的数据 +(nsmutabledata *)fromfilenamepathfetchlocaldata:(nsstring *)filename{ //保存数据到指定文件中 nsfilemanager *filemanager = [[nsfilemanager alloc] init]; if([filemanager fileexistsatpath:filename]){ nsdata *data = [filemanager contentsatpath:filename]; return [data mutablecopy]; } return nil; } //存储数据到指定文件 +(bool) storagedatatofile:(nsdata *)data filename:(nsstring *)filename{ //保存数据到指定文件中 nsfilemanager *filemanager = [[nsfilemanager alloc] init]; if([filemanager createfileatpath:filename contents:data attributes:nil]){ return yes; }else{ return no; } } //删除文件 +(void) deletefile:(nsstring *)filename{ nsfilemanager *filemanager = [[nsfilemanager alloc] init]; nserror *error; [filemanager removeitematpath:filename error:&error]; } //获取今天的日期 +(nsstring *) fetchtodaydate{ nsdate *currentdate = [nsdate date]; nsdateformatter *dateformatter = [[nsdateformatter alloc] init]; [dateformatter setdatestyle:nsdateformattermediumstyle]; return [dateformatter stringfromdate:currentdate]; } //获取昨天的日期 +(nsstring *) fetchyesterdaydate{ nsdate *yesterdaydate = [nsdate datewithtimeintervalsincenow:-(24 * 60 * 60)]; nsdateformatter *dateformatter = [[nsdateformatter alloc] init]; [dateformatter setdatestyle:nsdateformattermediumstyle]; return [dateformatter stringfromdate:yesterdaydate]; } //获取前天的日期 +(nsstring *) fetchyesterdaybeforedate{ nsdate *yesterdaydate = [nsdate datewithtimeintervalsincenow:-(2 * (24 * 60 * 60))]; nsdateformatter *dateformatter = [[nsdateformatter alloc] init]; [dateformatter setdatestyle:nsdateformattermediumstyle]; return [dateformatter stringfromdate:yesterdaydate]; } //获取存储文件的数据 //创建文件 //创建目录 +(nsstring *) createdirectory:(nsstring *)directoryname{ nsfilemanager *filemanager = [[nsfilemanager alloc] init]; nserror *error; if(![filemanager fileexistsatpath:directoryname]){ [filemanager createdirectoryatpath:directoryname withintermediatedirectories:yes attributes:nil error:&error]; if(error == nil){ return directoryname; }else{ return directoryname; } }else{ return directoryname; } } //删除文件 +(void) removefile:(nsstring *)filepath{ nserror *error; nsfilemanager *filemanager = [[nsfilemanager alloc] init]; if([filemanager fileexistsatpath:filepath]){ [filemanager removeitematpath:filepath error:&error]; } if(error){ nslog(@"error = %@",error); } } //删除目录 +(void) removedirectory{ nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentspath = [paths objectatindex:0]; nsstring *removedirectorypath = [documentspath stringbyappendingpathcomponent:[self fetchyesterdaybeforedate]]; nserror *error; nsfilemanager *filemanager = [[nsfilemanager alloc] init]; if([filemanager fileexistsatpath:removedirectorypath]){ [filemanager removeitematpath:removedirectorypath error:&error]; } if(error){ nslog(@"error = %@",error); } } @end storagedata.h // // storagedata.h // xunyi7 // // created by david on 13-6-28. // copyright (c) 2013年 david. all rights reserved. // #import <foundation/foundation.h> @interface storagedata : nsobject<nsurlconnectiondatadelegate, nsurlconnectiondelegate> +(nsmutabledata *)remotefetchdata:(nsstring *)dataurl; +(nsmutabledata *)localfetchdata:(nsstring *)dataurl; +(void) saveuploadimage:(uiimage *)image withname:(nsstring *)imagename; +(nsstring *) uploadimage:(uiimage *)image withname:(nsstring *)imagename; +(nsstring *) fetchuploadimagepath; +(nsstring *) fetchuploadimagepath:(nsstring *)imagename; +(void) removeuploadimage:(uiimage *)image withname:(nsstring *)imagename; +(nsstring *)isfileexists:(nsstring *)fullpath; +(void) removefile:(nsstring *)filepath; @end
有不完善的地方,希望指正和修改
上一篇: 京东技术三面+HR面,成功拿到30K offer入职京东
下一篇: IOS开发之路--C语言基础知识