// Nsfilemanager
# Import <Foundation/Foundation. h> # define path @ "/users/fengze/desktop" int main (INT argc, const char * argv []) {@ autoreleasepool {// create the File Manager object nsfilemanager * fm = [nsfilemanager defaultmanager]; // view the specified file path and return an array, nsarray * array = [FM contentsofdirectoryatpath: path error: Nil]; // the above method is just for shortest traversal, you cannot traverse the content in the folder. // you cannot traverse the content in the folder in depth. array = [FM subpathsofdirectoryatpath: path error: Nil]; // You can traverse the content in the current folder. In all the content, // create the directory [FM createdirectoryatpath: [nsstring stringwithformat: @ "% @/", path] withintermediatedirectories: No attributes: Nil error: Nil]; // create a dir file in the path directory // create a file [FM createfileatpath: [nsstring stringwithformat: @ "% @ dir", path] Contents: [@ "hello" datausingencoding: nsutf8stringencoding] attributes: Nil]; // create a hello file in the Dir folder, but what is the extended list file created? ///// Delete the object // [FM removeitematpath: [nsstring stringwithformat: @ "% @/DIR", path] error: Nil]; // delete all contents in the directory // copy the file [FM copyitematpath: [nsstring stringwithformat: @ "% @/DIR", path] topath: [nsstring stringwithformat: @ "% @/dir2", path] error: Nil]; // move the file [FM moveitematpath: [nsstring stringwithformat: @ "% @/DIR", path] topath: [nsstring stringwithformat: @ "% @/dir2", path] error: Nil];/* delete the file, copy the file, and move the file on the file, it can also be used on the folder */} return 0 ;}
Nshandle
# Import <Foundation/Foundation. h> // nshandle is called the file handle int main (INT argc, const char * argv []). {@ autoreleasepool {// open the file generation handle nsfilehandle * FH = [nsfilehandle filehandleforreadingatpath: @ "/users/fengze/desktop/test/"] in read-only mode; /* files from hard disk to memory are called reads, and from memory to hard disk are called writes. Memory is equivalent to brain, the hard disk is equivalent to reading the file content in the textbook * // nsdata * Data = [FH readdataoflength: 3]; Data = [FH readdataoflength: 5]; // The content read for the second time will continue to read the last read. // all content of the file is read at a time. nsstring * STR = [[nsstring alloc] initwithdata: data encoding: nsutf8stringencoding]; // open the file to generate the file handle nsfilehandle * fh2 = [nsfilehandle filehandleforwritingatpath: @ "/users/fengze/desktop/test"]; [fh2 writedata: [@ "hello" datausingencoding: nsutf8stringencoding]; // The written content will replace the content in front of the file, and the subsequent content will remain unchanged. // [fh2 truncatefileatoffset: 0]; // truncates the file content by 0 bytes (clear) // sets the read/write pointer to [FH seektoendoffile] at the end of the file; // append the content at the end of the file [fh2 writedata: [@ "XXX" datausingencoding: nsutf8stringencoding];} return 0 ;}
Dark Horse programmer ___ Foundation _ nsfilemanager and nsfilehandle