概述
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;