iOS文件操作

来源:互联网 发布:win6网络异常怎么修复 编辑:程序博客网 时间:2024/06/01 07:36

1.NSFileManager创建文件夹/文件; 拷贝; 删除; 剪切(move)

  1. 处理⽂文件系统的Foundation的类,提供对⽂文件或⺫⽬目录等的各种操作
  2. 主要功能
    • 向⽂文件写⼊入数据;从⽂文件中读取数据
    • 创建、复制、移动和删除⽂文件/⺫⽬目录
    • 测试⽂文件的存在性
    • 读取/更改⽂文件的属性
  3. 获取⽂文件管理器对象
    • defaultManager
  4. 创建⽂文件/⺫⽬目录
    • createFileAtPath:contents:attributes: -createDirectoryAtPath:withIntermediateDirectories:attributes:error
  5. 移动/剪切⽂文件
    • moveItemAtPath:toPath:error:
  6. 删除⽂文件
    • removeItemAtPath:error: - removeItemAtURL:error:
      NSFileManager *fm = [NSFileManagerdefaultManager];//初始化一个NSFileManager
      路径不能只给到文件夹级别应该具体到文件名
      copy:
      [fm copyItemAtPath:sourcePath toPath:destinationPath error:nil];
      move:
      [fm moveItemAtPath:sourcePath toPath:destinationPath error:nil];
      remove:
      [fm removeItemAtPath:filePath error:nil];
      判断一个路径对应的文件夹是否存在
      BOOL isExists = [fm fileExistsAtPath:directoryPath];
      在路径合法的情况下,判断这个路径是不是文件夹
      BOOL isDir;//表示是不是文件夹
      BOOL isExists = [fm fileExistsAtPath:path isDirectory:&isDir];//isExists表示路径存在不存在
      path hasPrefix:@“xx”//判断文件中是否含有xx
      path hasSuffix:@“xx”//判断文件尾是否含有xx
      path lastPathComponent//返回路径的最后一层
      [fm contentsOfDirectoryAtPath:path error:nil]//返回一个包含path路径下的所有文件名的数组

创建文件夹
参数:
//1.创建目录的完整路径描述
//2.是否把路径中不存在的文件都创建出来 NO 不存在不创建
//3.在iOS下 写nil OS X下表示 权限 等级 等参数
//4.捕获异常信息
NSError *error = nil;
[fm createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
if (error != nil) {
NSLog(@”%@”,[error localizedDescription]);
return;
}

创建文件
NSString *filePath = @”/Users/tedu/Desktop/ab/aaa.txt”;//
NSString *str = @”hello”;
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];//按utf—8方式存储str
[fm createFileAtPath:filePath contents:data attributes:nil];

NSString *newStr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; //把data转换成string
UIImage *image = [UIImage imageWithData:data];//把data转换成image

2.NSFileHandle
NSFileHandle: 文件句柄
是一个针对文件描述符的面向对象类, 对已存在的文件进行读,写和查找

NSFileHandle常⽤用⽅方法
1. 获取⽂文件读/写控制器对象
+ fileHandleForReadingAtPath: + fileHandleForWritingAtPath:
2. 写数据到⽂文件中 - writeData:
3. 从⽂文件中读取数据
- readDataToEndOfFile - readDataOfLength:
适应场景: 只能对已经存在的文件进行操作
//1.针对sourceFilePath创建写句柄
NSFileHandle *writeFileHandle = [NSFileHandle fileHandleForWritingAtPath:self.sourceFilePath];
NSString *content = @”写入测试内容”;
//2.写入content内容到sourceFilePath
[writeFileHandle writeData:[content dataUsingEncoding:NSUTF8StringEncoding]];
//3.关闭句柄
[writeFileHandle closeFile];

NSFileManager和NSFileHandle对⽐比
• NSFileManager类主要对⽂文件的管理和操作(新建/删除/修改/移动)
• NSFileHandle类主要对⽂文件的内容进⾏行读取和写⼊入

获取文件大小的两种方法
1.根据文件的文件属性字典获取
源文件的总长度(获取源文件的文件属性字典)
NSError *error = nil;
NSDictionary *sourceAttriDic = [self.fileMgr attributesOfItemAtPath:sourcePath error:&error];
NSNumber *sourceTotalSize = [sourceAttriDic objectForKey:NSFileSize];
NSInteger totalSize = [sourceTotalSize longValue];//源文件总长度
2.移动文件句柄的游标获取文件的长度
NSFileHandle *imageFH = [NSFileHandle fileHandleForReadingAtPath:filePath];
long long fileLength = [imageFH seekToEndOfFile];
imageFH seekToEndOfFile//移动到文件尾
imageFH seekToFileOffset:xxx//移动到制定处

0 0