The use of Nsfilehandle has been not proficient, inadvertently found the detailed introduction, showings convenient usage
Nsfilehandle This class is primarily read and write to the file contents
Nsfilemange This class is primarily the operation of files and the acquisition of file information
Common processing methods
+ (ID) Filehandleforreadingatpath: (NSString *) path opens a file ready to read
+ (ID) Filehandleforwritingatpath: (NSString *) path opens a file ready to write
+ (ID) Filehandleforupdatingatpath: (NSString *) path opens a file ready for update
-(NSData *) Availabledata; Returning available data from a device or channel
-(NSData *) readdatatoendoffile; Reads from the current node to the end of the file
-(NSData *) Readdataoflength: (nsuinteger) length; Reads the specified length data starting from the current node
-(void) WriteData: (NSData *) data; Write Data
-(unsigned long long) offsetinfile; Gets the offset of the current file
-(void) Seektofileoffset: (unsigned long long) offset; Jumps to the offset of the specified file
-(unsigned long long) seektoendoffile; Jump to end of file
-(void) Truncatefileatoffset: (unsigned long long) offset; Set the length of the file to offset bytes
-(void) closefile; Close File
Append data to a file
NSString *homepath = Nshomedirectory ();
NSString *sourcepath = [HomePath stringbyappendingpathconmpone:@ "Testfile.text"];
Nsfilehandle *fielhandle = [Nsfilehandle Filehandleforupdatingatpath:sourcepath];
[FileHandle Seektoendoffile]; To jump a node to the end of a file
NSString *str = @ "Append data"
nsdata* stringdata = [str datausingencoding:nsutf8stringencoding];
[FileHandle Writedata:stringdata]; Append Write Data
[FileHandle CloseFile];
Locating data
Nsfilemanager *FM = [Nsfilemanager Defaultmanager];
NSString *content = @ "abcdef";
[FM Createfileatpath:path contents:[content datausingencoding:nsutf8stringencoding] attributes:nil];
Nsfilehandle *filehandle = [Nsfilehandle Filehandleforreadingatpath:path];
Nsuinteger length = [filehandle availabeldata] length]; Get Data length
[FileHandle SEEKTOFILEOFFSET;LENGTH/2]; Half of the offset file
NSData *data = [FileHandle readdatatoendoffile];
[FileHandle CloseFile];
Copying files
Nsfilehandle *infile, *outfile; Input file, output file
NSData *buffer; Buffered data read
Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];
NSString *homepath = Nshomedirectory ();
NSString *sourcepath = [HomePath stringbyappendingpathcomponent:@ "testfile.txt"]; Source file path
NSString *outpath = [HomePath stringbyappendingpathcomponent:@ "Outfile.txt"]; Output file path
BOOL sucess = [FileManager createfileatpath:outpath contents:nil Attributes:nil];
if (!success)
{
return N0;
}
infile = [Nsfilehandle Filehandleforreadingatpath:sourcepath]; Create read source Path file
if (infile = = nil)
{
return NO;
}
outfile = [Nsfilehandle Filehandleforreadingatpath:outpath]; Create a disease open file to output
if (outfile = = nil)
{
return NO;
}
[OutFile truncatefileatoffset:0]; Set the length of the output file to 0
Buffer = [infile readdatatoendoffile]; Reading data
[OutFile Writedata:buffer]; Write input
[InFile CloseFile]; Close write, input file
[OutFile CloseFile];
Nsfilehandle, Nsfilemange