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