[Objective-C] Introduction to NSFileHandle in OC and common usage

Source: Internet
Author: User

NSFileHandle

NSFileManager class is mainly used for file operations (such as deleting, modifying, moving, and assigning values)

The NSFileHandle class mainly reads and writes the file content.

NSFileHandle

1: Create an NSFileHandle object

2: perform I/O operations on open files

3. Disable object operations.

Common Handling Methods

 

+ (Id) fileHandleForReadingAtPath :( NSString *) path; // open a file to prepare for reading + (id) fileHandleForWritingAtPath :( NSString *) path; // open a file to prepare for writing + (id) fileHandleForUpdatingAtPath :( NSString *) path; // open a file and update (read, write)-(NSData *) availableData; // return available data-(NSData *) readDataToEndOfFile; // read data from the current node location to the end of the file-(NSData *) readDataOfLength :( NSUInteger) length; // read data of the specified length from the current node location-(void) writeData :( NSData *) data; // write data-(unsigned long) offsetInFile; // obtain the offset of the current file-(unsigned long) seekToEndOfFile; // jump to the end of the file-(void) seekToFileOffset :( unsigned long) offset; // jump to the specified offset position of the specified file-(void) truncateFileAtOffset :( unsigned long) offset; // set the file length-(void) synchronizeFile; // file synchronization-(void) closeFile; // close the file

 

Instance code

1 (add data to the file :):

@ Autoreleasepool {
NSString * homePath = NSHomeDirectory (); NSLog (@ "% @", homePath); NSString * filePath = [homePath stringByAppendingFormat: @ "/Desktop/testfile"]; NSLog (@ "% @", filePath); NSFileHandle * fileHandle = [NSFileHandle fileHandleForUpdatingAtPath: filePath]; [fileHandle seekToEndOfFile]; NSString * str = @ "The data added to the test is "; NSData * data = [str dataUsingEncoding: NSUTF8StringEncoding]; [fileHandle writeData: data]; [fileHandle closeFile];} return 0;

 

 

2: locate the data in the file:

 

          NSString *homePath=NSHomeDirectory();          NSString *filePath=[homePath stringByAppendingFormat:@"/Desktop/testfile"];          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);
 
[Here are some methods of the NSData class]
3: copy the data in the file
// Copy the NSString * homePath = NSHomeDirectory (); NSString * filePath = [homePath stringByAppendingFormat: @ "/Desktop/testfile"]; // NSFileHandle * fileHandle = [NSFileHandle handle: filePath]; NSString * targetPath = [homePath stringByAppendingFormat: @ "/Desktop/outfile"]; NSFileManager * fileManager = [NSFileManager defaultManager]; BOOL result = [fileManager createFileAtPath: targetPath contents: nil Attributes: nil]; if (result) {NSLog (@ "create success! ");} NSFileHandle * inFileHandle = [NSFileHandle fileHandleForReadingAtPath: filePath]; NSFileHandle * outFileHandle = [NSFileHandle handle: targetPath]; NSData * inData = [inFileHandle availableData] // read all the data in the file. // write the file [outFileHandle writeData: inData]; [inFileHandle closeFile]; [outFileHandle closeFile];

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.