With the file operation handle, which is an object such as nsfilehandle, we can operate the file more closely. Generally, the following three steps are required to operate a file:
- Open the file and create the file operation handle for the file;
- Input/Output operations;
- Close the file.
The following table summarizes the measures that can be taken for file operation handles:
| Action name |
Description |
| + (Nsfilehandle *) filehandleforreadingatpath: Path |
Open the file to read |
| + (Nsfilehandle *) filehandleforwritingatpath: Path |
Open the file for writing |
| + (Nsfilehandle *) filehandleforupdatingatpath: Path |
Open a file to read and write |
| -(Nsdata *) availabledata |
The result is the data available in the implementation object. |
| -(Nsdata *) readdatatoendoffile |
Read data before the end of the file |
| -(Nsdata *) readdataoflength :( nsuinteger) bytes |
Reads data of bytes. |
| -(Void) writedata: Data |
Write data to a file |
| -(Unsigned long) offsetinfile |
Obtain the operation location in the current file |
| -(Void) seektofileoffset: Offset |
Set the operation position of the current file to offset |
| -(Unsigned long) seektoendoffile |
Set the operation location of the current file to the end of the file. |
| -(Void) truncatefileatoffset: Offset |
Set the file length to offset. |
| -(Void) closefile |
Close file |
Note: you cannot create a file using the file operation handle. You can only use the File Manager (nsfilemanager) to create a file. Therefore, to send filehandleforwritingatpath: And filehandleforupdatingatpath to objects such as nsfilehandle, the premise of the two messages is that the file must exist. If the file does not exist, nsfilehandle will generate a null value nil. In both cases, the file operation location is set at the beginning of the file. In addition, if you are used to programming under UNIX, you should note that opening a file cannot shorten the file. You have to shorten the file by yourself.
The following program demonstrates how to use the file operation handle to operate a file:
1 # import <Foundation/nsobject. h> 2 # import <Foundation/nsstring. h> 3 # import <Foundation/nsfilehandle. h> 4 # import <Foundation/nsfilemanager. h> 5 # import <Foundation/NSAID utoreleasepool. h> 6 # import <Foundation/nsdata. h> 7 int main (INT argc, const char * argv []) 8 {9 ngutoreleasepool * Pool = [[ngutoreleasepool alloc] init]; 10 nsfilehandle * infile, * OUTFILE; 11 nsdata * buffer; 12 // create a file operation handle for the file testfile for reading Data 13 infile = [nsfilehandle filehandleforreadingatpath: @ "testfile"]; 14 if (infile = nil) 15 {16 nslog (@ "failed to open the file! "); 17 return 1; 18} 19 // create the file testfile220 [[nsfilemanager defaultmanager] createfileatpath: @" testfile2 "Contents: Nil attributes: Nil]; 21 // create a file operation handle for file testfile2 to write data 22 OUTFILE = [nsfilehandle filehandleforwritingatpath: @ "testfile2"]; 23 if (OUTFILE = nil) 24 {25 nslog (@ "An error occurred while opening the file! "); 26 return 2; 27} 28 // cut the length of file testfile2 to 0 29 [OUTFILE truncatefileatoffset: 0]; 30 // read the data in file testfile to buffer 31 buffer = [infile readdatatoendoffile]; 32 // write the data in buffer to file testfile2 33 [OUTFILE writedata: buffer]; 34 // close two files 35 [infile closefile]; 36 [OUTFILE closefile]; 37 // display the content of testfile2, used to verify whether the previous operation was successful 38 nslog (@ "% @", [nsstring stringwithcontentsoffile: @ "testfile2" encoding: nsutf8stringencoding error: Nil]); 39 [pool drain]; 40 return 0; 41}
After running this program, you can get the following results:
1 15:51:06. 184 sample [1507: 707] programming is very interesting! 2 program ended with exit code: 0
Readdatatoendoffile: this measure can read up to 1099511627542 bytes of data. This is big enough for any program you write. You can set a circular buffer to transfer data between files. This can be done through readdataoflength. The buffer size can be 8192 bytes or 131072 bytes, because the underlying operating system generally uses data blocks of this size to perform input/output operations.
If a measure does not read any data until the end of the file, it will generate an empty nsdata object. You can take the Length Measure on this nsdata object to check whether any data is read from the file.
If you open a file for reading and writing, the file operation location will be set at the beginning of the file. You can set the file operation location to another location, and then perform read and write operations. For example, you can set the operation location of the file to 10th bytes:
1 [myfilehandle seektofileoffset: 10];
First obtain the current file operation location, and then add or subtract a certain number of bytes to the file operation location, we can get a relative file operation location. For example, skip the 128 bytes forward based on the current file operation location, as shown in the following code:
1 [myfilehandleseektofileoffset: [myfilehandleoffsetinfile] + 128];
To move the operating position of the current file back to the length of five integers, we can use the following statement:
1 [myfilehandleseektofileoffset: [myfilehandleoffsetinfile]-5 * sizeof (INT)];