File Manager:
1 #import "ViewController.h"2 3 @interfaceViewcontroller ()4 5 @end6 7 @implementationViewcontroller8 9- (void) Viewdidload {Ten [Super Viewdidload]; One //additional setup after loading the view, typically from a nib. A - #pragmaMark-File Manager creation - //Creating Objects theNsfilemanager *manager =[Nsfilemanager Defaultmanager]; - - //Create Path -NSString *path =nshomedirectory (); + -Path = [path stringbyappendingpathcomponent:@"Test/myapp"]; +NSLog (@"Path =%@", path); ABOOL success =[manager Createdirectoryatpath:path Withintermediatedirectories:yes Attributes:nil Error:nil]; at if(Success) { -NSLog (@"file creation succeeded"); -}Else { -NSLog (@"File creation failed"); - } - inNSString *path2 = [path stringbyappendingpathcomponent:@"Test2/myapp"]; -NSLog (@"======%@", path2); to [manager createdirectoryatpath:path2 Withintermediatedirectories:yes Attributes:nil Error:nil]; + - #pragmaMark-File Manager add the //to add a string to a folder *Path = [path stringbyappendingpathcomponent:@"Zifuchuan.txt"]; $ Panax Notoginseng //Initializes a string -NSString *string=@"today is a number-one entry ."; theBOOL Success1 = [stringwritetofile:path atomically:yes encoding:nsutf8stringencoding Error:nil]; + if(SUCCESS1) { ANSLog (@"Success"); theNSLog (@"path=========%@", path); +}Else { -NSLog (@"failed"); $ } $ -Path = [path2 stringbyappendingpathcomponent:@"New.text"]; -NSString *newstr =@"Just write some."; the [newstr writetofile:path atomically:yes encoding:nsutf8stringencoding Error:nil]; -NSLog (@"-------%@", path);Wuyi the #pragmaMark-File Manager Delete -[Manager Removeitematpath:path Error:nil];//Delete the document under this folder, the folder cannot be deleted Wu #pragmaMark-File Manager Mobile, copy - About //Documents Path $NSString *documentpath = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, NSUserDomainMask, YES) Objectatindex:0]; - - //Create a folder stringbydeletinglastpathcomponent delete the last part of the content -NSString *copypath = [Documentpath stringbyappendingpathcomponent:@"Backup/text.txt"]; A [manager Createdirectoryatpath:[copypath Stringbydeletinglastpathcomponent] Withintermediatedirectories:yes Attributes:nil Error:nil]; +NSString *str =@"txt text when writing content inside"; theNSData *data = [str datausingencoding:nsutf8stringencoding];//convert string to data type - [manager Createfileatpath:copypath Contents:data Attributes:nil]; $NSLog (@"-=-=-=-=-=%@", Copypath); the the //Create a Topath to move/copy the contents of the Copypath to Topath theNSString *topath = [Documentpath stringbyappendingpathcomponent:@"Hello/wode.txt"]; the - [manager Createdirectoryatpath:[topath Stringbydeletinglastpathcomponent] Withintermediatedirectories:yes Attributes:nil Error:nil]; in //Moving the //[manager Moveitematpath:copypath Topath:topath Error:nil]; theNSLog (@"Topath =%@", Topath); About //Copy the [manager Copyitematpath:copypath Topath:topath Error:nil]; the the } + - @end
File Docking Device:
1 @interfaceViewcontroller ()2 3 @end4 5 @implementationViewcontroller6 7- (void) Viewdidload {8 [Super Viewdidload];9 //additional setup after loading the view, typically from a nib.Ten #pragmaMark-Create a file using Nsfilemessage OneNsfilemanager *filemanager =[Nsfilemanager Defaultmanager]; ANSString *documentpath =[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) firstobject]; -NSString *strpath = [Documentpath stringbyappendingpathcomponent:@"file.txt"]; - the[FileManager Createfileatpath:strpath contents:[@"Hello"datausingencoding:nsutf8stringencoding] Attributes:nil]; - -NSLog (@"%@", strpath); - + #pragmaMark-Append content to file using Nsfilehandle - //Create a Handle object +Nsfilehandle *filehandle =[Nsfilehandle Filehandleforupdatingatpath:strpath]; A //Search to end of text content at [FileHandle seektoendoffile]; - -NSString *appendstr =@"I'm handle's content."; - [FileHandle writedata:[appendstr datausingencoding:nsutf8stringencoding]; - - [FileHandle CloseFile]; in - #pragmaMark-Positioning data to + //write "123456" to the File2.txt folder -NSString * content =@"123456"; theNSString * filePath2 = [Documentpath stringbyappendingpathcomponent:@"File2.txt"]; * [FileManager createfileatpath:filepath2 contents:[content datausingencoding:nsutf8stringencoding] Attributes: NIL]; $ Panax Notoginseng //Read through FileHandle -FileHandle =[Nsfilehandle filehandleforreadingatpath:filepath2]; the //Get Data length +Nsuinteger length =[[FileHandle availabledata] length]; A //half of the offset file the[FileHandle seektofileoffset:length/2.0]; + //read from half position to last -NSData * data =[FileHandle readdatatoendoffile]; $ [FileHandle CloseFile]; $ //print a read string -NSString *string=[[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]; -NSLog (@"%@",string); the - }Wuyi the- (void) didreceivememorywarning { - [Super didreceivememorywarning]; Wu //Dispose of any resources the can be recreated. - } About $ @end
iOS File Manager and file docking device