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]; |