简单实例NSFileHandle的文件读写
2015-12-12 17:16
423 查看
新建helo文件,初始内容123456789,经过多次修改,输出相应内容。
输出结果:
// // main.m /* 演示文件句柄的使用:读文件和写文件操作 */ #import <Foundation/Foundation.h> #define PATH @"/Users/apple/Documents/ios_dev/test_case/TestFileHandle" int main(int argc, const char * argv[]) { @autoreleasepool { /* 新建文件和初始化文本内容 */ NSString *path = [NSString stringWithFormat:@"%@/hello.txt", PATH]; NSData *content = [@"123456789" dataUsingEncoding:NSUTF8StringEncoding]; NSFileManager *fm = [NSFileManager defaultManager]; BOOL ret = [fm createFileAtPath:path contents:content attributes:nil]; if (ret == 0) { NSLog(@"Create file fail."); return 0; } /* 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. fileHandleForReadingAtPath:Returns a file handle initialized for reading the file, device, or named socket at the specified path. */ NSString *str1 = [NSString stringWithFormat:@"%@/hello.txt",PATH]; NSFileHandle *rfh = [NSFileHandle fileHandleForReadingAtPath:str1]; /* readDataOfLength:Synchronously reads data up to the specified number of bytes. */ NSData *da1 = [rfh readDataOfLength:2]; /* 句柄位置变动 */ /* readDataToEndOfFile:Synchronously reads the available data up to the end of file or maximum number of bytes. */ NSData *da2 = [rfh readDataToEndOfFile]; /* 从上次句柄处开始读取 */ NSString *str2 = [[NSString alloc] initWithData:da1 encoding:NSUTF8StringEncoding]; NSLog(@"first read contents:%@", str2); NSString *str3 = [[NSString alloc] initWithData:da2 encoding:NSUTF8StringEncoding]; NSLog(@"seconde read contents:%@", str3); /* fileHandleForWritingAtPath:Returns a file handle initialized for writing to the file, device, or named socket at the specified path. */ NSFileHandle *wfh = [NSFileHandle fileHandleForWritingAtPath:path]; NSData *da3 = [@"BEST&" dataUsingEncoding:NSUTF8StringEncoding]; /* writeData:If the receiver is a file, writing takes place at the file pointer’s current position. After it writes the data, the method advances the file pointer by the number of bytes written.*/ [wfh writeData:da3]; /* seekToEndOfFile:Moves the file pointer to the specified offset within the file represented by the receiver. */ [rfh seekToFileOffset:0]; NSData *da4 = [rfh readDataToEndOfFile]; NSString *str4 = [[NSString alloc] initWithData:da4 encoding:NSUTF8StringEncoding]; NSLog(@"read contents after first write:%@", str4); /* 注意此时wfh的句柄不在文件首位置,而在执行了写入da3后的那个位置,因此不是从文件首或文件末尾写入新的字符串 */ NSData *da5 = [@"##" dataUsingEncoding:NSUTF8StringEncoding]; [wfh writeData:da5]; [rfh seekToFileOffset:0]; da4 = [rfh readDataToEndOfFile]; str4 = [[NSString alloc] initWithData:da4 encoding:NSUTF8StringEncoding]; NSLog(@"read contents after second write:%@", str4); /* 从文件末尾处新增内容 */ [wfh seekToEndOfFile]; da5 = [@"hero" dataUsingEncoding:NSUTF8StringEncoding]; [wfh writeData:da5]; [rfh seekToFileOffset:0]; da4 = [rfh readDataToEndOfFile]; str4 = [[NSString alloc] initWithData:da4 encoding:NSUTF8StringEncoding]; NSLog(@"read contents after third write:%@", str4); } return 0; }
输出结果:
2015-12-12 18:41:46.570 TestFileHandle[836:39034] first read contents:12 2015-12-12 18:41:46.571 TestFileHandle[836:39034] seconde read contents:3456789 2015-12-12 18:41:46.571 TestFileHandle[836:39034] read contents after first write:BEST&6789 2015-12-12 18:41:46.572 TestFileHandle[836:39034] read contents after second write:BEST#89 2015-12-12 18:41:46.572 TestFileHandle[836:39034] read contents after third write:BEST#89hero
相关文章推荐
- 纯代码实现CSS圆角
- R连接mysql时,已列出所有表,错误Error in odbcTableExists(channel, sqtable) object 'wuliudata' not found解决方案
- 单机搭建基于Hadoop的Spark环境
- objective-c延展类目协议
- python基础教程共60课-第33课处理文件中的数据
- hackthegame<第一关>
- 运算符、表达式和语句
- [leetcode 204] Count Primes
- 遥感影像处理
- 软件测试与软件质量-什么是软件测试
- android-Creating a Search Interface
- Java中的逆变与协变
- python学习吐槽
- cocoaPods的安装
- 遥感影像处理
- Android进阶:获取屏幕参数
- Objective-C中copy属性的概述
- 在状态栏上显示会闪烁的图标(类似qq消息提示)
- 【比较】构件图和部署图
- 时间格式转换