IOS NSFileHandle, iosnsfilehandle
NSFileHandle is mainly used to read and write the file content.
NSFileMange is mainly used to perform operations on files and obtain file information.
Common Handling Methods
+ (Id) fileHandleForReadingAtPath :( NSString *) path open a file to prepare for reading
+ (Id) fileHandleForWritingAtPath :( NSString *) path to open a file to prepare for writing
+ (Id) fileHandleForUpdatingAtPath :( NSString *) path to open a file and prepare for update
-(NSData *) availableData; returns available data from the device or channel
-(NSData *) readDataToEndOfFile; read from the current node to the end of the file
-(NSData *) readDataOfLength :( NSUInteger) length; read the specified length data from the current node
-(Void) writeData :( NSData *) data; write data
-(Unsigned long) offsetInFile; get the offset of the current file
-(Void) seekToFileOffset :( unsigned long) offset; jump to the offset of the specified file
-(Unsigned long) seekToEndOfFile; jump to the end of the file
-(Void) truncateFileAtOffset :( unsigned long) offset; set the object length to offset bytes.
-(Void) closeFile; close the file
Append data to a file
NSString * homePath = NSHomeDirectory ();
NSString * sourcePath = [homePath stringByAppendingPathConmpone: @ "testfile. text"];
NSFileHandle * fielHandle = [NSFileHandle fileHandleForUpdatingAtPath: sourcePath];
[FileHandle seekToEndOfFile]; jump the node to the end of the file
NSString * str = @ "append data"
NSData * stringData = [str dataUsingEncoding: NSUTF8StringEncoding];
[FileHandle writeData: stringData]; append and write data
[FileHandle closeFile];
Locate data
NSFileManager * fm = [NSFileManager defamanager manager];
NSString * content = @ "abcdef ";
[Fm createFileAtPath: path contents: [content dataUsingEncoding: NSUTF8StringEncoding] attributes: nil];
NSFileHandle * fileHandle = [NSFileHandle fileHandleForReadingAtPath: path];
NSUInteger length = [fileHandle availabelData] length]; get the Data length
[FileHandle seekToFileOffset; length/2]; half of the Offset File
NSData * data = [fileHandle readDataToEndOfFile];
[FileHandle closeFile];
Copy a file
NSFileHandle * infile, * outfile; input file and output file
NSData * buffer; read buffer data
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 a file to read The Source Path
If (infile = nil)
{
Return NO;
}
Outfile = [NSFileHandle fileHandleForReadingAtPath: outPath]; create a disease to open the file to be output
If (outfile = nil)
{
Return NO;
}
[Outfile truncateFileAtOffset: 0]; set the length of the output file to 0
Buffer = [infile readDataToEndOfFile]; read data
[Outfile writeData: buffer]; write Input
[Infile closeFile]; close writing and input files
[Outfile closeFile];