------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------
Nsfilemanager
#import <Foundation/Foundation.h> #define PATH @ "/users/fengze/desktop" int main (int argc, const char * argv[]) {@ Autoreleasepool {//Create File Manager Object Nsfilemanager *fm = [Nsfilemanager Defaultmanager]; View the specified file path and return an array of directories storing files and folders Nsarray *array = [FM contentsofdirectoryatpath:path Error:nil]; The above method is only a shallow traversal, can not traverse the contents of the folder//depth traversal array = [FM Subpathsofdirectoryatpath:path E Rror:nil]; You can iterate through all the contents of the current folder//create directory [FM createdirectoryatpath:[nsstring stringwithformat:@ "%@/Exercise", PATH ] Withintermediatedirectories:no Attributes:nil Error:nil]; The above meaning is to create a dir file in the PATH directory//create a file [FM createfileatpath:[nsstring stringwithformat:@ "% @dir", PATH] Conte nts:[@ "Hello" datausingencoding:nsutf8stringencoding] attributes:nil]; A hello file was created in the Dir folder, but what is the extension list file created? //deletion of files//[FM removeitematpath:[nsstring stringwithformat:@]%@/dIR ", PATH] error:nil]; Will delete all contents of the directory//file copy [FM copyitematpath:[nsstring stringwithformat:@ "%@/dir", PATH] Topath:[nsstrin G stringwithformat:@ "%@/dir2", PATH] error:nil]; File movement [FM moveitematpath:[nsstring stringwithformat:@ "%@/dir", PATH] topath:[nsstring stringwithformat:@ "% @/dir2 ", PATH] error:nil]; /* File deletion, file copy, file movement can be used both on the file, or can be used on the folder */} return 0;
Nshandle
#import <foundation/foundation.h>//nshandle called file handle int main (int argc, const char * argv[]) {@autoreleasepool { Open file generation file handle in read-only mode nsfilehandle *FH = [Nsfilehandle filehandleforreadingatpath:@ "/users/fengze/desktop /test/"]; /* files from hard disk to memory called read, from memory to hard disk called write, memory equivalent to brain, hard disk equivalent to textbook */////Read file contents according to specified range nsdata *data = [FH Read Dataoflength:3]; data = [FH Readdataoflength:5]; The second read will then continue reading the last read//Read all the contents of the file one time nsstring *str = [[NSString alloc] Initwithdata:data encoding : Nsutf8stringencoding]; Open file generation file handle in write-only mode nsfilehandle *FH2 = [Nsfilehandle filehandleforwritingatpath:@ "/users/fengze/desktop/test"]; [Fh2 writedata:[@ "Hello" datausingencoding:nsutf8stringencoding]]; The content written will replace the previous contents of the file, and the contents of the following are unchanged//[FH2 truncatefileatoffset:0]; Truncate the contents of the file by 0 bytes (empty)//Set the read-write pointer to the end of the file [FH Seektoendoffile]; Append content to the end of the file [fh2 writedata:[@ "xxx" datausingencoding:nsutf8stringencoding]; } return 0;}
Dark Horse programmer--foundation--nsfilemanager and Nsfilehandle