First, Nsfilehandle
The Nsfilemanager class mainly operates on files (delete, modify, move, copy, etc.)
The Nsfilehandle class mainly reads and writes the contents of a file
Second, the Nsfilehandle class processes the file the step
Create a Nsfilehandle object
To i/0 an open file
Close File
You can use Nsfilehandle to make a breakpoint continuation
Third, the code to implement the lookup function:
NSString *homepath=nshomedirectory ();
NSString *filepath=[homepath stringbyappendingpathcomponent:@ "Desktop/hello.rtf"];
Nsfilehandle *filehandle=[nsfilehandle Filehandleforreadingatpath:filepath];
Nsuinteger Length=[filehandle availabledata].length;
[FileHandle SEEKTOFILEOFFSET:LENGTH/2];
NSData *data=[filehandle Readdatatoendoffile];
NSString *str=[[nsstring alloc] Initwithdata:data encoding:nsutf8stringencoding];
NSLog (@ "%@", str);
Implementation of append file code:
NSString *homepath=nshomedirectory ();
NSString *filepath=[homepath stringbyappendingpathcomponent:@ "Desktop/hello.rtf"];
Nsfilehandle *filehandle=[nsfilehandle Filehandleforupdatingatpath:filepath];
[FileHandle Seektoendoffile];
[FileHandle Seektofileoffset:10];
NSString *str=@ "Data";
NSData *data=[str datausingencoding:nsutf8stringencoding];
[FileHandle Writedata:data];
[FileHandle CloseFile];
Insert code here ...
NSLog (@ "Hello, world!");
The code to implement the copied file:
NSString *homepath=nshomedirectory ();
NSString *filepath=[homepath stringbyappendingpathcomponent:@ "Desktop/hello.rtf"];
NSString *objpath=[homepath stringbyappendingpathcomponent:@ "Desktop/copy.rtf"];
Nsfilemanager *filemanager=[nsfilemanager Defaultmanager];
BOOL Success=[filemanager Createfileatpath:objpath Contents:nil Attributes:nil];
Nsfilehandle *writefile=[nsfilehandle Filehandleforreadingatpath:filepath];
Nsfilehandle *objfile=[nsfilehandle Filehandleforwritingatpath:objpath];
[objFILE Readdatatoendoffile];
NSData *data=[writefile Readdatatoendoffile];
[objFILE Writedata:data];
[WriteFile CloseFile];
[objFILE CloseFile];