Objective-C OC中文件读取类(NSFileHandle)介绍和常用使用方法

    • 转自http://www.it165.net/pro/html/201402/9100.html
    • NSFileHandle

      NSFileManager类主要对于文件的操作(删除,修改,移动,赋值等等)

      NSFileHandle类主要对文件的内容进行读取和写入操作

      NSFileHandle处理文件的步骤

      1:创建一个NSFileHandle对象

      2:对打开的文件进行I/O操作

      3:关闭文件对象操作

      常用处理方法

       

      01. + (id)fileHandleForReadingAtPath:(NSString *)path; //打开一个文件准备读取
      02. + (id)fileHandleForWritingAtPath:(NSString *)path;  //打开一个文件准备写入
      03. + (id)fileHandleForUpdatingAtPath:(NSString *)path; //打开一个文件可以更新(读取,写入)
      04. - (NSData *)availableData;      //返回可用的数据
      05. - (NSData *)readDataToEndOfFile;  //从当前的节点位置读取到文件末尾
      06. - (NSData *)readDataOfLength:(NSUInteger)length; //从当前的节点位置开始读取指定长度的数据
      07. - (void)writeData:(NSData *)data;    //写入数据
      08. - (unsigned long long)offsetInFile;   //获取当前文件的偏移量
      09. - (unsigned long long)seekToEndOfFile; //跳转到文件结尾
      10. - (void)seekToFileOffset:(unsigned long long)offset; //跳转到指定文件的指定的偏移量的位置
      11. - (void)truncateFileAtOffset:(unsigned long long)offset;  //设置文件长度
      12. - (void)synchronizeFile;  //文件同步
      13. - (void)closeFile;   //关闭文件

       

      实例代码

      1(对文件进行加入数据:):

      @autoreleasepool {
      01. NSString *homePath=NSHomeDirectory();
      02. NSLog(@"%@",homePath);
      03.  
      04. NSString *filePath=[homePath stringByAppendingFormat:@"/Desktop/testfile"];
      05. NSLog(@"%@",filePath);
      06. NSFileHandle *fileHandle=[NSFileHandle fileHandleForUpdatingAtPath:filePath];
      07.  
      08. [fileHandle seekToEndOfFile];
      09. NSString *str=@"测试加入的数据为";
      10. NSData *data=[str dataUsingEncoding:NSUTF8StringEncoding];
      11. [fileHandle writeData:data];
      12. [fileHandle closeFile];
      13. }
      14. return 0;
      \

       

       

      2:对文件中的数据进行定位:

       

      1. NSString *homePath=NSHomeDirectory();
      2. NSString *filePath=[homePath stringByAppendingFormat:@"/Desktop/testfile"];
      3. NSFileHandle *fileHandle=[NSFileHandle fileHandleForReadingAtPath:filePath];
      4. NSUInteger length= [fileHandle availableData].length;
      5. [fileHandle seekToFileOffset:length/2];
      6. NSData *data=[fileHandle readDataToEndOfFile];
      7. NSString *str=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
      8. NSLog(@"%@",str);
      \
      1. [特别讲一下NSData类的一些方法]
      \
      1. 3:复制文件中的数据
      1. //复制文件 NSString *homePath=NSHomeDirectory(); NSString *filePath=[homePath stringByAppendingFormat:@"/Desktop/testfile"]; //NSFileHandle *fileHandle=[NSFileHandle fileHandleForReadingAtPath:filePath]; NSString *targetPath=[homePath stringByAppendingFormat:@"/Desktop/outfile"]; NSFileManager *fileManager=[NSFileManager defaultManager]; BOOL result=[fileManager createFileAtPath:targetPath contents:nil attributes:nil]; if(result){ NSLog(@"create success!"); } NSFileHandle *inFileHandle=[NSFileHandle fileHandleForReadingAtPath:filePath]; NSFileHandle *outFileHandle=[NSFileHandle fileHandleForWritingAtPath:targetPath]; NSData *inData=[inFileHandle availableData]; //读出文件中所有的数据 //下面开始进行写文件 [outFileHandle writeData:inData]; [inFileHandle closeFile]; [outFileHandle closeFile];

转载于:https://www.cnblogs.com/ZhangYuGe/p/4503275.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值