1. Simple and in-depth file traversal:
// Nsfilemanager * fm = [[nsfilemanager alloc] init]; // create a file manager nserror * error = nil; // display the content in the path. The function is similar to the LS-a command // The returned value is to put the content in the Directory into nsarray // traverse nsfilemanager in a shortest * fm = [nsfilemanager defamanager]; nsarray * array = [FM contentsofdirectoryatpath: @ "/users/AAAAA/desktop" error: & error]; // nil error returned if (error) {nslog (@ "error = % @", error); Return-1; // indicates that the function returns (end function) // exit (1 ); // end the current process} else {nslog (@ "array = % @", array );} // in-depth traversal // nsarray * array1 = [FM subpathsofdirectoryatpath: @ "/users/AAAAA/desktop" error: & error]; If (error) {nslog (@ "error = % @", error);} else {nslog (@ "array1 = % @", array1 );}
2. Create a directory:
Nsfilemanager * fm = [nsfilemanager defaultmanager]; nserror * error = nil; // The current path refers to the path of the executable program rather than the path of the Code. // create the Directory and return the value: if yes is returned, nobool ret = [FM createdirectoryatpath: @ "is returned :@". /hello2/World "// path of the Created directory withintermediatedirectories: Yes // The path does not exist. Whether to create a directory is yes (a nonexistent directory is created). No means not to create attributes: nil // file attributes (permissions). Generally, writing nil indicates the default permission error: & error]; If (RET) {nslog (@ "success! ");} Else {nslog (@" error = % @ ", error );}
Create a file
Nsfilemanager * fm = [nsfilemanager defaultmanager]; // create a file bool ret = [FM createfileatpath :@". /1.txt" // path and file name contents: Nil // content of the file to be created nil indicates that the file is empty and overwrites the file attributes: Nil] If it exists. [FM fileexistsatpath: @". /1.txt"] // determines whether a file exists.
3. copy the file/directory:
Nsfilemanager * fm = [nsfilemanager defaultmanager]; nserror * error = nil; // copy a file/directory // The second parameter must contain the entire file or the directory name bool ret = [FM copyitematpath: @ "/users/AAAAA/desktop/string. C "// path of the file to be copied topath :@". /1.txt" // target file (do not add spaces to the file name). If it exists, an error: & error] is returned.
4. Move (rename) the file/directory:
// Rename (MOVE) the file or directory // rename: Modify the file path // if the file/directory fails to be moved, ret = [FM moveitematpath: @ "/users/AAAAA/desktop/" topath :@". /1/1 "// enter the full name of the parameter error: & error];
5. delete the file/directory:
/Delete a file/directory // use it with caution (direct deletion will not be thrown into the waste bin) ret = [FM removeitematpath: @ "./1.txt" error: & error];
6. nsdata and string:
Nsstring * STR = @ "Hello World"; // convert the OC string to the nsdata type. // nsdata * Data in the nsdata File Buffer = [STR datausingencoding: nsutf8stringencoding]; // convert the nsdata type to the nsstring type nsstring * str2 = [[nsstring alloc] initwithdata: data encoding: nsutf8stringencoding];
7. file read/write operations:
// File read/write operations // Step 1 open the file // file * fp = fopen (". /1.txt", "W"); file pointer // file handle // open nsfilehandle * FH = [nsfilehandle filehandleforreadingatpath: @ "/users/AAAAA/desktop/string in read-only mode. C "]; // open FH = [nsfilehandle filehandleforwritingatpath: @"/users/AAAAA/desktop/string in write-only mode. C "]; // enable FH = [nsfilehandle filehandleforupdatingatpath: @"/users/AAAAA/desktop/string in read/write mode. C "]; // The OPEN function of OC will locate the offset at the beginning of the file. // if you want to operate at the end of the file, you can locate the file offset at the end of the file [FH seektoendoffile]; // locate the end Of the file nsdata * data1 = [FH readdataoflength: 10]; // read the specified number of bytes nsdata * data2 = [FH readdatatoendoffile]; // read the end Of the file from the current offset [FH seektofileoffset: 10]; // locate the file offset to 10 bytes.
8. Write Data to the file:
Nsfilehandle * fh2 = [nsfilehandle filehandleforupdatingatpath :@". /1.txt"]; nsstring * STR = @ "Hello World"; nsdata * Data = [STR datausingencoding: nsutf8stringencoding]; // write data must be converted to nsdata [fh2 writedata: data];
9. Get the file length:
// Obtain the file attributes through the File Manager and then obtain the file length through the dictionary nsfilemanager * fm = [nsfilemanager defaultmanager]; nsdictionary * dict = [FM attributesofitematpath: @ "/users/AAAAA/desktop/string. C "error: & error]; // Method 1: nslog (@" size = % LLD ", [dict filesize]); // Method 2: nslog (@ "% @", [dict objectforkey: nsfilesize]);
OC file operations (1)