Data read/write-NSFileHandle

Source: Internet
Author: User

Overview NSFileHandle class is an object-oriented encapsulation of a file description. You can use a file handle object to access data related to files, sockets, pipelines, and devices. You can read and write files. For sockets, pipelines, and devices, you can use a file handle object to monitor asynchronous device and process data. (The NSFileHandle class is an object-oriented wrapper for a file descriptor. you use file handle objects to access data associated with files, sockets, pipes, and devices. for files, you can read, write, and seek within the file. for sockets, pipes, and devices, you can use a file handle object to monitor the device and process data asynchronously .) common methods and methods: fileHandleForUpdatingAtPath: Update file fileHand LeForReadingAtPath: Read File fileHandleForWritingAtPath: Write File and method of the corresponding operation URL instance method writeData: Write Data readDataToEndOfFile read to the end of the file readDataOfLength: read the specified length of data seekToEndOfFile and move it to the end of the file. closeFile closes the file availableData and returns the currently available data. We noticed that because the NSFileHandle class does not provide file creation, deletion, and other related operations, therefore, NSFileManager is also required for these file operations. A simple Demo shows the stored data information in the text view. The preview view is as follows: Interface part [cpp] @ property (weak, nonatomic) IBOutlet UITextField * nameField; @ property (weak, nonatomic) IBOutlet UITextField * ageField; @ property (weak, nonatomic) extends UITextField * emailField; @ property (weak, nonatomic) IBOutlet UITextView * informationView) saveInformation :( id) sender;-(IBAction) loadInformation :( id) sender;-(IBAction) tappedEndEdi Ting :( id) sender;-(IBAction) tapped :( id) sender; @ property (weak, nonatomic) IBOutlet UITextField * nameField; @ property (weak, nonatomic) IBOutlet UITextField * ageField; @ property (weak, nonatomic) IBOutlet UITextField * emailField; @ property (weak, nonatomic) IBOutlet UITextView * informationView;-(IBAction) saveInformation :( id) sender;-(IBAction) loadInformation :( id) sender;-(IBAction) tappedEndEditing :( Id) sender;-(IBAction) tapped :( id) sender; implements data storage [cpp]-(IBAction) saveInformation :( id) sender {NSString * inputString = [NSString stringWithFormat: @ "% @-% @ \ n", self. nameField. text, self. ageField. text, self. emailField. text]; NSString * docDir = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) [0]; NSString * filePath = [docDir stringByAppendingPathComponent: @ "myInf OList.csv "]; NSFileManager * fileManager = [NSFileManager defaultManager]; if (! [FileManager fileExistsAtPath: filePath]) {[fileManager createFileAtPath: filePath contents: nil attributes: nil]; NSLog (@ "File Created successfully");} NSFileHandle * fileHandle = [NSFileHandle handle: filePath]; [fileHandle seekToEndOfFile]; [fileHandle writeData: [inputString dataUsingEncoding: NSUTF8StringEncoding]; [fileHandle closeFile]; self. nameField. text = @ ""; self. ageField. text = @ ""; self. EmailField. text = @ "";}-(IBAction) saveInformation :( id) sender {NSString * inputString = [NSString stringWithFormat: @ "% @-% @ \ n ", self. nameField. text, self. ageField. text, self. emailField. text]; NSString * docDir = plain (NSDocumentDirectory, NSUserDomainMask, YES) [0]; NSString * filePath = [docDir stringByAppendingPathComponent: @ "myInfoList.csv"]; NSFileManager * fi LeManager = [NSFileManager defaultManager]; if (! [FileManager fileExistsAtPath: filePath]) {[fileManager createFileAtPath: filePath contents: nil attributes: nil]; NSLog (@ "File Created successfully");} NSFileHandle * fileHandle = [NSFileHandle handle: filePath]; [fileHandle seekToEndOfFile]; [fileHandle writeData: [inputString dataUsingEncoding: NSUTF8StringEncoding]; [fileHandle closeFile]; self. nameField. text = @ ""; self. ageField. text = @ ""; self. EmailField. text = @ "";} implements read data [cpp]-(IBAction) loadInformation :( id) sender {NSString * docDir, * filePath; NSFileManager * fileManager; docDir = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) [0]; filePath = [docDir stringByAppendingPathComponent: @ "myInfoList.csv"]; fileManager = [NSFileManager ultmanager]; if ([fileManager fileExistsAtPath: filePath]) {NSLog (@ "exists This file "); NSFileHandle * fileHandle; fileHandle = [NSFileHandle handle: filePath]; NSString * outputString = [[NSString alloc] initWithData: [fileHandle availableData] encoding: handle]; [fileHandle closeFile]; self. informationView. text = outputString ;}}- (IBAction) loadInformation :( id) sender {NSString * docDir, * filePath; NSFileManager * fileManager; docDir = NSSearchPa Alias (NSDocumentDirectory, NSUserDomainMask, YES) [0]; filePath = [docDir example: @ "myInfoList.csv"]; fileManager = [NSFileManager defamanager manager]; if ([fileManager fileExistsAtPath: filePath]) {NSLog (@ "this file exists"); NSFileHandle * fileHandle; fileHandle = [NSFileHandle fileHandleForReadingAtPath: filePath]; NSString * outputString = [[NSString alloc] initWithData: [FileHandle availableData] encoding: NSUTF8StringEncoding]; [fileHandle closeFile]; self. informationView. text = outputString ;}implement other methods [cpp]-(IBAction) tappedEndEditing :( id) sender {[self. view endEditing: YES];}-(IBAction) tapped :( id) sender {self. informationView. text = @ "";}-(IBAction) tappedEndEditing :( id) sender {[self. view endEditing: YES];}-(IBAction) tapped :( id) sender {self. informationVi Ew. text = @ "";} a hidden keyboard and a gesture operation.

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.