File Manager Nsfilemanager
1 #pragmaMark-File Manager Create folder2 3 //Creating Objects4Nsfilemanager *manager =[Nsfilemanager Defaultmanager];5 6 //Create Path7NSString *path =nshomedirectory ();8Path = [path stringbyappendingpathcomponent:@"Test/myapp"];9NSLog (@"%@", path);Ten One //Create ANserror *error =Nil; -BOOL success = [Manager Createdirectoryatpath:path withintermediatedirectories:yes Attributes:nil error:&ERROR]; -NSLog (@"success =%d, error =%@", success, error); the - - #pragmaMark-File Manager Add content - //Add a String +Path = [path stringbyappendingpathcomponent:@"Hhh.txt"]; - + //Initialize string ANSString *string=@"haha haha"; at -[stringwritetofile:path atomically:yes encoding:nsutf8stringencoding Error:nil]; - - - #pragmaMark-File Manager Delete content - [manager Removeitematpath:path Error:nil]; in - to #pragmaMark-File Manager Mobile, copy + //Find Documents Path -NSString *documentpath = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, NSUserDomainMask, YES) Objectatindex:0]; the * $ //Create a folderPanax NotoginsengNSString *copypath = [Documentpath stringbyappendingpathcomponent:@"Backup/test.txt"]; - the //Stringbydeletinglastpathcomponent Delete the last part of the content + [manager Createdirectoryatpath:[copypath Stringbydeletinglastpathcomponent] Withintermediatedirectories:yes Attributes:nil Error:nil]; A the //Define a string +NSString *teststr =@"ha ha haha"; -NSData *data =[Teststr datausingencoding:nsutf8stringencoding]; $ $ [manager Createfileatpath:copypath Contents:data Attributes:nil]; - - the //Create a topath[to move] -NSString *topath = [Documentpath stringbyappendingpathcomponent:@"Hello/aaa.txt"];Wuyi the [manager Createdirectoryatpath:[topath Stringbydeletinglastpathcomponent] Withintermediatedirectories:yes Attributes:nil Error:nil]; - WuNserror *error1 =Nil; -BOOL result = [Manager Moveitematpath:copypath Topath:topath error:&Error1]; AboutNSLog (@"result =%d, Error1 =%@", result, error1); $ - -BOOL RESULT1 =[manager Copyitematpath:copypath Topath:topath Error:nil]; -NSLog (@"%d", RESULT1);
File docking device Nsfilehandle
1 #pragmaMark-Create a folder with Nsfilemanager2Nsfilemanager *filemanager =[Nsfilemanager Defaultmanager];3 4NSString *documentpath = [Nshomedirectory () stringByAppendingPathComponent:@"Documents"];5NSString *strpath = [Documentpath stringbyappendingpathcomponent:@"file.txt"];6 7[FileManager Createfileatpath:strpath contents:[@"Hello"datausingencoding:nsutf8stringencoding] Attributes:nil];8NSLog (@"%@", strpath);9 Ten One #pragmaMark-Append content to file using Nsfilehandle A //Create a Handle object -Nsfilehandle *filehandle =[Nsfilehandle Filehandleforupdatingatpath:strpath]; - the //Search to end of text content - [FileHandle seektoendoffile]; - - + //Create Data -NSString *appendstr =@"I'm a new addition."; +NSData *data1 =[Appendstr datausingencoding:nsutf8stringencoding]; A at //writing data to the docking device - [FileHandle writedata:data1]; - - //Close the docking device - [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);
File Manager and file docking device