数据读写——NSFileHandle(2)
来源:未知 责任编辑:责任编辑 发表时间:2014-02-02 17:44 点击:次
p>
p> NSString *filePath = [docDir stringByAppendingPathComponent:@"myInfoList.csv"];
p>
p> NSFileManager *fileManager = [NSFileManager defaultManager];
p> if (![fileManager fileExistsAtPath:filePath]) {
p> [fileManager createFileAtPath:filePath contents:nil attributes:nil];
p> NSLog(@"文件创建成功");
p> }
p>
p> NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
p> [fileHandle seekToEndOfFile];
p> [fileHandle writeData:[inputString dataUsingEncoding:NSUTF8StringEncoding]];
p> [fileHandle closeFile];
p>
p> self.nameField.text = @"";
p> self.ageField.text = @"";
p> self.emailField.text = @"";
p>}
p>
p>- (IBAction)saveInformation:(id)sender {
p> NSString *inputString = [NSString stringWithFormat:@"%@ - %@ - %@\n",
p> self.nameField.text, self.ageField.text, self.emailField.text];
p>
p> NSString *docDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
p>
p> NSString *filePath = [docDir stringByAppendingPathComponent:@"myInfoList.csv"];
p>
p> NSFileManager *fileManager = [NSFileManager defaultManager];
p> if (![fileManager fileExistsAtPath:filePath]) {
p> [fileManager createFileAtPath:filePath contents:nil attributes:nil];
p> NSLog(@"文件创建成功");
p> }
p>
p> NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
p> [fileHandle seekToEndOfFile];
p> [fileHandle writeData:[inputString dataUsingEncoding:NSUTF8StringEncoding]];
p> [fileHandle closeFile];
p>
p> self.nameField.text = @"";
p> self.ageField.text = @"";
p> self.emailField.text = @"";
p>}
p>
p>
p>实现读取数据
p>[cpp]
p>- (IBAction)loadInformation:(id)sender {
p> NSString *docDir, *filePath;
p> NSFileManager *fileManager;
p>
p> docDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
p> filePath = [docDir stringByAppendingPathComponent:@"myInfoList.csv"];
相关新闻>>
- 发表评论
-
- 最新评论 更多>>