iOS Access sandbox directory API
Get the program directory
NSString *homepath = Nshomedirectory ();
~/library/application Support/iphone simulator/7.1/applications/66095245-fd69-40d5-b3f5-9594a6dc6862
Get the Documents Library tmp Library/cache directory
Nsarray *array = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSString *documentpath = [array firstobject]; NSLog (@ "%@", documentpath); Cache directory array = nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES); NSString *cachespath = [array firstobject]; NSLog (@ "%@", Cachespath); Library array = nssearchpathfordirectoriesindomains (Nslibrarydirectory,nsuserdomainmask,yes); NSString *librarypath = [array firstobject]; NSLog (@ "%@", LibraryPath); TMP NSString *tmppath = Nstemporarydirectory (); NSLog (@ "%@", Tmppath);
The reading and writing of the array in the file
Write array to file nsarray *writearray = @[@ "Zhang San", @ "John Doe", @ "Harry"]; [Writearray writetofile:[documentpath stringbyappendingpathcomponent:@ "test.txt"] atomically:yes]; Read file to array nsarray *readarray = [Nsarray arraywithcontentsoffile:[documentpath stringbyappendingpathcomponent:@ " Test.txt "];
atomically: If the write atomicity of the file is guaranteed for Yes, a temporary file is created until the file content is successfully written and then imported into the destination file.
If no, it is written directly to the target file.
File operation via Nsfilemanager
Creating a file with Nsfilemanager
//Create folders and files under documentNSString *testdirectory = [Documentpath stringbyappendingpathcomponent:@"Test"]; NSString*test1 = [Testdirectory stringbyappendingpathcomponent:@"Test1.txt"]; NSString*test2 = [Testdirectory stringbyappendingpathcomponent:@"Test2.txt"]; NSString*TEST3 = [Testdirectory stringbyappendingpathcomponent:@"Test3.txt"]; Nsfilemanager*manager =[Nsfilemanager Defaultmanager]; [Manager createdirectoryatpath:testdirectory Withintermediatedirectories:yes Attributes:nil Error:nil]; NSString*text =@"Hello, IOS"; [Manager Createfileatpath:test1 Contents:[text datausingencoding:nsutf8stringencoding] attributes:nil]; [Manager Createfileatpath:test2 Contents:[text datausingencoding:nsutf8stringencoding] attributes:nil]; [Manager createfileatpath:test3 Contents:[text datausingencoding:nsutf8stringencoding] attributes:nil];
Traversal of files via Nsfilemanager
// Read all the file names, the two methods have the same effect Nsarray *files1 = [manager subpathsatpath:testdirectory]; *files2 = [manager subpathsofdirectoryatpath:testdirectory Error:nil];
Manipulating the current directory with Nsfilemanager
[manager Changecurrentdirectorypath:documentpath]; Nsarray*filearray = [[Nsarray alloc] Initwithobjects:@"Hello World",@"Hello world1",@"Hello world2", nil]; [Manager Createfileatpath:@"TestFileNSFileManager.txt"Contents:filearray Attributes:nil]; [Manager Createfileatpath:@"TestFileNSFileManager1.txt"Contents:filearray Attributes:nil];
Delete Files via Nsfilemanager
[Manager removeItemAtPath:@ "testFileNSFileManager1.txt" Error:nil];
File IO operation via Nsfilehandle
Nsfilehandle cannot create files, so file creation also has to go through nsfilemanager,nsfilehandle Open file if not present will return nil
//create three files under document, copy all the contents of file 1 and the contents of file 2 to file 3, then delete file 1;Nsarray *array =nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSString*documentpath =[Array firstobject]; Nsfilemanager*manager =[Nsfilemanager Defaultmanager]; NSString*path1 = [Documentpath stringbyappendingpathcomponent:@"Text1.txt"]; NSString*path2 = [Documentpath stringbyappendingpathcomponent:@"Text2.txt"]; NSString*path3 = [Documentpath stringbyappendingpathcomponent:@"Text3.txt"]; //Create 3 Files[Manager Createfileatpath:path1 contents:[@"Hello from Text1"datausingencoding:nsutf8stringencoding] Attributes:nil]; [Manager createfileatpath:path2 contents:[@"Hello from Text2"datausingencoding:nsutf8stringencoding] Attributes:nil]; [Manager Createfileatpath:path3 contents:[@"Hello from Text3"datausingencoding:nsutf8stringencoding] Attributes:nil]; //Get Nsfilehandle ObjectNsfilehandle *path1handle =[Nsfilehandle filehandleforreadingatpath:path1]; Nsfilehandle*path2handle =[Nsfilehandle filehandleforreadingatpath:path2]; Nsfilehandle*path3handle =[Nsfilehandle Filehandleforwritingatpath:path3]; //IO read/writeNSData *text1data =[Path1handle Readdatatoendoffile]; NSData*text2data = [Path2handle readdataoflength:5]; //move the file pointer of the Text3 file to the last[Path3handle Seektoendoffile]; Nsmutabledata*data =[Nsmutabledata Datawithdata:text1data]; [Data Appenddata:text2data]; [Path3handle Writedata:data]; //Close the Nsfilehandle object[Path1handle CloseFile]; [Path2handle CloseFile]; [Path3handle CloseFile]; //Delete Text1[Manager Removeitematpath:path1 Error:nil];
NSBundle handling Project-related resources
http://blog.csdn.net/iphoneing/article/details/5872610
iOS file operations