概述 NSFileHandle類是一種物件導向的封裝對一個檔案的描述。您可以使用檔案控制代碼對象來訪問檔案,通訊端,管道和裝置相關的資料。對於檔案,您可以在檔案中讀,寫。對於通訊端,管道和裝置,你可以使用一個檔案控制代碼對象來監視裝置和過程資料的非同步。 (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.) 常用方法類方法 fileHandleForUpdatingAtPath:更新檔案fileHandleForReadingAtPath:讀檔案fileHandleForWritingAtPath:寫檔案 以及相應的操作URL的方法 執行個體方法 writeData:寫資料readDataToEndOfFile讀到檔案末尾readDataOfLength:讀取指定長度的資料seekToEndOfFile移到檔案末尾closeFile關閉檔案availableData返回當前可用的資料 我們注意到由於NSFileHandle類並沒有提供檔案的檔案的建立刪除等相關操作,所以這些對檔案的操作還需要藉助NSFileManager來執行。 一個簡單的Demo這個樣本在文本視圖中顯示存入的資料資訊,預覽視圖如下: 介面部分[cpp] @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; @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; 實現儲存資料[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:@"myInfoList.csv"]; NSFileManager *fileManager = [NSFileManager defaultManager]; if (![fileManager fileExistsAtPath:filePath]) { [fileManager createFileAtPath:filePath contents:nil attributes:nil]; NSLog(@"檔案建立成功"); } NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath: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 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; NSString *filePath = [docDir stringByAppendingPathComponent:@"myInfoList.csv"]; NSFileManager *fileManager = [NSFileManager defaultManager]; if (![fileManager fileExistsAtPath:filePath]) { [fileManager createFileAtPath:filePath contents:nil attributes:nil]; NSLog(@"檔案建立成功"); } NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:filePath]; [fileHandle seekToEndOfFile]; [fileHandle writeData:[inputString dataUsingEncoding:NSUTF8StringEncoding]]; [fileHandle closeFile]; self.nameField.text = @""; self.ageField.text = @""; self.emailField.text = @"";} 實現讀取資料[cpp] - (IBAction)loadInformation:(id)sender { NSString *docDir, *filePath; NSFileManager *fileManager; docDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; filePath = [docDir stringByAppendingPathComponent:@"myInfoList.csv"]; fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath:filePath]) { NSLog(@"存在該檔案"); NSFileHandle *fileHandle; fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath]; NSString *outputString = [[NSString alloc] initWithData:[fileHandle availableData] encoding:NSUTF8StringEncoding]; [fileHandle closeFile]; self.informationView.text = outputString; } } - (IBAction)loadInformation:(id)sender { NSString *docDir, *filePath; NSFileManager *fileManager; docDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; filePath = [docDir stringByAppendingPathComponent:@"myInfoList.csv"]; fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath:filePath]) { NSLog(@"存在該檔案"); NSFileHandle *fileHandle; fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath]; NSString *outputString = [[NSString alloc] initWithData:[fileHandle availableData] encoding:NSUTF8StringEncoding]; [fileHandle closeFile]; self.informationView.text = outputString; }} 實現其他方法[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.informationView.text = @"";}一個隱藏鍵盤,一個手勢操作。