資料讀寫——NSFileHandle

來源:互聯網
上載者:User

概述  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 = @"";}一個隱藏鍵盤,一個手勢操作。  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.