#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib. //Nsfilemanager Simple to use, File management class//1. Create an object using the Defaultmanager class method, create a singleton object (the singleton of the system and Nsuserdefaults, Nsnotification, Uiapplication,nsurlcache);Nsfilemanager*filemanger =[Nsfilemanager Defaultmanager]; //Create a folder and stitch the/path1/path2/file you want to create into the path//Get Documents PathNsarray *paths =nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSString*documentpath = [Paths Objectatindex:0]; NSLog (@"path:%@", documentpath); NSString*newpath1 = [Documentpath stringbyappendingpathcomponent:@"path1"]; [Filemanger createdirectoryatpath:newpath1 withintermediatedirectories:yes Attributes:nil Error:nil]; //create a file inside a folderNSString *filepath1 = [newPath1 stringbyappendingpathcomponent:@"Text.txt"]; BOOL file=[Filemanger createfileatpath:filepath1 contents:nil Attributes:nil]; if(file) {NSLog (@"file creation succeeded"); }Else{NSLog (@"File creation failed"); } //write to file (can also write dictionary, array, etc.)NSString *strdata =@"want to write something"; BOOL Write=[strdata writetofile:filepath1 atomically:yes encoding:nsutf8stringencoding Error:nil]; if(write) {NSLog (@"Write Success"); }Else{NSLog (@"Write Failed"); } //determine if a file exists if([Filemanger fileexistsatpath:filepath1]) {NSLog (@"the file exists"); }Else{NSLog (@"The file does not exist"); } //Deleting FilesBOOL Delete =[Filemanger removeitematpath:filepath1 Error:nil]; if(delete) {NSLog (@"file deletion succeeded"); }Else{NSLog (@"File deletion failed"); } }- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end
Simple and practical Nsfilemanager