method One : This section of Objective C code is used to delete the file of the specified path
if ([FileManager removeitematpath:@ "FilePath" Error:null]) {
NSLog (@ "removed successfully");
}
Method Two:
Nsfilemanager *defaultmanager;
Defaultmanager = [Nsfilemanager Defaultmanager];
[Defaultmanager removefileatpath:tildefilename
Handler:nil];
Handler can receive messages, such as if the deletion fails, you can use Filemanager:shouldproceedaftererror:.
Method Three:
IOS Delete File Delete folder Create file Create folder determine file exists MD5 encapsulation class
I have recently been using data access and deletion, so I wrote a functional class, their own use or quite convenient to share again
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 (@ "
Start knot Didreceivedata search Data "); }-(void) connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response{NSLog (@ "
Start knot didreceiveresponse search Data ");
}-(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]];
Create directory 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:nsurlrequestuseprotoc
Olcachepolicy 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]];
Create directory 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) {//Local data return localdata;
}else{//remote FETCH data Nsmutabledata *receiveddata = [self remotefetchdata:dataurl]; if (Receiveddata!= nil) {if ([self storagedatatofile:receiveddata filename:currentdatafilepath]) {NSLog (@) is saved as
Work ");
[Self removedirectory]; }else{NSLog (@ "save Failed"); }}else{if (Localdata = [self Fromfilenamepathfetchlocaldata:yesterdaydatafilepath])!= nil) {return lo
Caldata;
} return receiveddata;
return nil;
//MD5 Encrypted string + (NSString *) MD5: (NSString *) str{const char *CSTR = [Str utf8string];
unsigned char result[16]; CC_MD5 (CStr, strlen (CStr), result); This is the MD5 called 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], re
SULT[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15]; //Upload picture storage + (void) Saveuploadimage: (UIImage *) image withname: (NSString *) imagename{nsfilemanager = [[*filemanager
Emanager alloc] init];
Nserror *error;
Get the sandbox directory nsstring *fullpath = [Nshomedirectory () stringbyappendingpathcomponent:@ "Documents"]; FullPath = [FullPath stringbyappendingpathcomponent:@ "Tmpimage"]; if (![ FileManager Fileexistsatpath:fullpath]) {[FileManager Createdirectoryatpath:fullpath WithIntermediateDirectori
Es:yes Attributes:nil error:&error];
} fullpath = [FullPath stringbyappendingpathcomponent:imagename];
NSData *imagedata = uiimagejpegrepresentation (image, 0.5);
Write the picture to the file [ImageData Writetofile:fullpath Atomically:no]; //Upload picture deletion + (void) Removeuploadimage: (UIImage *) image withname: (NSString *) imagename{nsfilemanager = [[NS
FileManager alloc] init];
Nserror *error;
Get the sandbox directory nsstring *fullpath = [Nshomedirectory () stringbyappendingpathcomponent:@ "Documents"];
FullPath = [FullPath stringbyappendingpathcomponent:@ "Tmpimage"]; if (![
FileManager Fileexistsatpath:fullpath]) {[FileManager Removeitematpath:fullpath error:&error]; }//Get stored picture + (NSString *) Fetchuploadimagepath: (NSString *) imagename{nsstring *fullPath = [Nshomedirectory () stringbyappendingpathcomponent:@ "Documents"];
FullPath = [FullPath stringbyappendingpathcomponent:@ "Tmpimage"];
FullPath = [FullPath stringbyappendingpathcomponent:imagename];
return fullpath;
//Determine if the file exists + (NSString *) isfileexists: (NSString *) fullpath{Nsfilemanager *filemanager = [[Nsfilemanager alloc] init];
if ([FileManager Fileexistsatpath:fullpath]) {return fullpath;
return nil; }//Data storage//+ (void)//Get directory of stored files + (NSString *) datapath{//Here first specify the image access path (default write to Application sandbox) Nsarray *paths = nssearchpathfor
Directoriesindomains (Nsdocumentdirectory,nsuserdomainmask, YES);
and give the file a filename nsstring *filepathderectory = [Paths objectatindex:0];
return filepathderectory; //Get the data of the specified file + (Nsmutabledata *) Fromfilenamepathfetchlocaldata: (NSString *) filename{//Save the data to the specified file Nsfilemanager *file
Manager = [[Nsfilemanager alloc] init];
if ([FileManager Fileexistsatpath:filename]) {NSData *data = [FileManager contentsatpath:filename]; RetUrn [data mutablecopy];
return nil; ///Store data to specified file + (BOOL) Storagedatatofile: (NSData *) data fileName: (NSString *) filename{//Save data to specified file Nsfilemanager *fil
Emanager = [[Nsfilemanager alloc] init];
if ([FileManager createfileatpath:filename contents:data Attributes:nil]) {return YES;
}else{return NO;
}//delete file + (void) DeleteFile: (NSString *) filename{Nsfilemanager *filemanager = [[Nsfilemanager alloc] init];
Nserror *error;
[FileManager removeitematpath:filename error:&error];
//Get today's Date + (NSString *) fetchtodaydate{nsdate *currentdate = [NSDate Date];
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
[Dateformatter Setdatestyle:nsdateformattermediumstyle];
return [Dateformatter stringfromdate:currentdate]; //Get yesterday's Date + (NSString *) fetchyesterdaydate{nsdate *yesterdaydate = [NSDate datewithtimeintervalsincenow:-(24 * 60 *
60)];
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init]; [Dateformatter SetdatestYle:nsdateformattermediumstyle];
return [Dateformatter stringfromdate:yesterdaydate]; //Get the day before yesterday + (NSString *) fetchyesterdaybeforedate{nsdate *yesterdaydate = [NSDate datewithtimeintervalsincenow:-(2 *
(24 * 60 * 60))];
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
[Dateformatter Setdatestyle:nsdateformattermediumstyle];
return [Dateformatter stringfromdate:yesterdaydate]; //Get data to store files//Create files//create directories + (NSString *) CreateDirectory: (NSString *) directoryname{Nsfilemanager *filemanager = [[N
Sfilemanager alloc] init];
Nserror *error; if (![ FileManager Fileexistsatpath:directoryname]) {[FileManager createdirectoryatpath:directoryname WithIntermediat
Edirectories:yes Attributes:nil error:&error];
if (Error = = nil) {return directoryname;
}else{return directoryname;
}}else{return directoryname;
}//delete file + (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); }//delete directory + (void) removedirectory{Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, NSUserDom
Ainmask, 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
There are imperfections where you want to correct and modify