Project Day02 文件的相关操作

NSFileManger:对整个文件进行操作《手机文件在沙箱中不能直接操作》


删除文件

步骤:

1 创建NSFileManger的实例

2. 创建NSerror的实例初始值为空

3. 调用removeItemAtPath方法第一个参数为要删除的文件的位置第二个error的参数设置为&error的地址

4. 错误判断 如果出错 可以调用[error  localizedDescription]返回系统提供给的描述

NSFileManagerNSFileManagerdefaultManager];
NSError*error=nil;
removeItemAtPath:@"/Users/tarena6/Desktop/Filer Manager/124"error:&error];
if (error){
NSLog(@"文件错误:%@",[error localizedDescription]);
    }

复制文件

方法一:
[mangaercopyItemAtURL:[NSURLfileURLWithPath:@"/Users/tarena6/Desktop/FilerManager/123456.zip"] toURL:[NSURL fileURLWithPath:@"/Users/tarena6/Desktop/FilerManager/123456/11.zip"] error:nil];
 
方法二:
[mangaercopyItemAtPath:@"/Users/tarena6/Desktop/FilerManager/123456.zip" toPath:@"/Users/tarena6/Desktop/FilerManager/123456/222.zip" error:nil];

注意:在copy时  复制到的具体位置要写清楚 ,并且写清楚复制完后保存的文件名称


移动文件

方法一:
[mangaermoveItemAtPath:@"/Users/tarena6/Desktop/FilerManager/1.text" toPath:@"/Users/tarena6/Desktop/FilerManager/123456/11.text" error:nil];
方法二:
[mangaermoveItemAtURL:[NSURLfileURLWithPath:@"/Users/tarena6/Desktop/FilerManager/1.tex"] toURL:[NSURL fileURLWithPath:@"/Users/tarena6/Desktop/FilerManager/123456/13221.text"] error:nil];

注意:移动也要具体倒某个文件


创建文件

方法:

1. 创建一个nsstring的类里面写入要存的数据内容

2. 创建NSData的实例 值为string调用dataUSingEnabled转码得到

3.调用Manger的creatFileAtPath方法contents为nsdata的实例

NSString*name=@"张三";
NSData*data=[name dataUsingEncoding:NSUTF8StringEncoding];
createFileAtPath:@"/Users/tarena6/Desktop/Filer Manager/1.text"contents:data attributes:nil];

创建文件夹

创建文件夹 第二个参数用来控制路径中如果有 不存在的文件夹的时候是否 创建成功
 [managercreateDirectoryAtPath:@"/Users/tarena/Desktop/b/a"withIntermediateDirectories:NO attributes:nil error:nil]; 
判断文件是否存在
  判断是否文件存在
    if ([managerfileExistsAtPath:@"/Users/tarena/Desktop/1.zip"]) {
存在");
不存在");
判断是否是文件夹
NO;
if ([manager fileExistsAtPath:@"/Users/tarena/Desktop/test2.rtf"isDirectory:&isDir]&&isDir) {
NSLog(@"文件存在并且是文件夹");
        
else{
NSLog(@"文件不存在或不是文件夹");
    }

获取文件夹下面的所有文件

1.获取遍历的根目录

2. 通过contentsOfDirectoryAtPath方法得到该路径下的所有文件名称  放入到数组中

3.遍历数组得到每个文件的名称

4.通过stringByAppendingPathComponent拼接字符串会自动加上一个/

NSString*direcotry = @"/Users/tarena/Desktop/未命名文件夹";
NSArray*fileNames = [manager contentsOfDirectoryAtPath:direcotryerror:nil];
for (NSString *fileName infileNames) {
NSLog(@"%@",fileName);
//使用下面的方法拼接字符串会自动添加一个 /
NSString*filePath = [direcotry stringByAppendingPathComponent:fileName];
删除该文件
removeItemAtPath:filePatherror:nil];
    }


fileName  hasPreFix:@“.”带有某些.开头的文件一般是隐藏文件 即查询所有的隐藏文件

filePath   hasSuffix:@“jpg”路径的最后带有jpg的文件  即查询所有的jpg文件



NSFileHandle

1. 创建fileHandle的实例   NSFileHandle *fh = [NSFileHandle fileHandleForReadingAtPath:@/..];

2.通过fileLength获取长度 不会把文件加载倒内存当中 int fileLength=(int)[fn seekToEndOfFile]拿到长度

    **使用nsData创建的会加载到内存当中

3. 把游标定位到起始位置:

            [fn seekToFileOffset:0]//其实位置 可以是长度的一半  几分之几

4.定义一个nsdate的实例值为[fn readDataToEndOfFile]读取该文件到结束然后把结果村给nsdata的实例

5.创建一个与上面文件相同的文件 可以是image 可以是文本文件  也可以是配置文件等等

    对于image来说

   创建iamgeView的实例  调用【uiimageimageWithData:….】实现把nsdata的实例传给imageview 的image

6 添加到新创建的文件名字可以不同但是后缀必须相同

7.如果使用fileHandle写数据的时候必须保证文件存在

NSFileHandleNSFileHandlefileHandleForWritingAtPath:@"/Users/a.jpg"];

8.设置要写的开始位置或者说找到一个卡尺的位置 [writeFHseekToFileOffset:fileLength/2];

9.将nsdata的实例写入到fileHandle的实例中

NSFileHandle*fh = [NSFileHandle fileHandleForReadingAtPath:@"/Library/Screen Savers/DefaultCollections/2-Aerial/Aerial06.jpg"];
  
  1得到文件的长度
//   1.Data获取此时会把文件加载到内存中
//    NSData *data = [NSDatadataWithContentsOfFile:@"/Users/tarena/Documents/UIKitCore/Day1DragImage/Day1DragImage/CatInBin.jpg"];
//    int length = data.length;
//2.通过fileHandel获取长度不会把文件加载到内存
intfileLength = (int)[fh seekToEndOfFile];
//把游标重置到开始的位置
seekToFileOffset:fileLength/2];
    
NSData*subData = [fh readDataToEndOfFile];
    
UIImageView*iv = [[UIImageView alloc]initWithFrame:self.view.bounds];
image= [UIImage imageWithData:subData];
self.view addSubview:iv];
//创建文件
//    [[NSFileManagerdefaultManager]createFileAtPath:@"/Users/tarena/Desktop/未命名文件夹/a.jpg" contents:nil attributes:nil];
//如果使用fileHandle写数据的时候必须保证目标文件存在
NSFileHandle*writeFH = [NSFileHandle fileHandleForWritingAtPath:@"/Users/tarena/Desktop/未命名文件夹/a.jpg"];
seekToFileOffset:fileLength/2];
writeData:subData];