• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
    • 转自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 *[email protected]"测试加入的数据为";
      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];


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap