您的位置:首页 > 其它

Oc-语句总结(3)--NSFileManager 文件管理器

2016-02-04 22:13 453 查看
1.是否可存,是否属于目录,是否可读,是否可写,可删除

2.按照递归(自己调用自己)方式获取子目录

NSString
*str = @"we,are,famulei";

        BOOL
result = [str
writeToFile:@"/Users/mac/Desktop/1.txt"
atomically:YES
];

        if(result){
NSLog(@"写入成功");}else{NSLog(@"失败");}
           //文件管理器

  //1.文件管理器的创建
       
NSFileManager *fm = [NSFileManager
defaultManager];

       
       
   //2.判断一个文件是否存在,参数是文件路径
      
BOOL result1 =  [fm
fileExistsAtPath:@"/Users/mac/Desktop/1.txt"];

        if
(result1) {

            NSLog(@"文件存在");

        }else{NSLog(@"文件不存在");}

   

       
   
//3.判断一个文件是否存在,且文件是否属于目录
       
BOOL isDir;

        //参数2
表示该文件是否

        BOOL
result2 = [fm
fileExistsAtPath:@"/Users/mac/Desktop/1.txt"
isDirectory:&isDir];

             //判断是否为文件

        if
(result2) {

            NSLog(@"文件存在");

        }else{NSLog(@"文件不存在");}

              //判断是否为目录

        if
(isDir) {

            NSLog(@"为文件夹-目录");

        }else{NSLog(@"只是一个单纯的文件");}

   

    //4.判断一个文件是否可读

        BOOL
result3 = [fm
isReadableFileAtPath:@"/Users/mac/Desktop/1.txt"];

        if
(result3) {

            NSLog(@"文件可读");

        }else{NSLog(@"文件不可读");}

   

   
       
//5.判断一个文件是否可写
        result3 = [fm
isWritableFileAtPath:@"/Users/mac/Desktop/1.txt"];

        if
(result3) {

            NSLog(@"文件可写");

        }else{NSLog(@"文件不可写");}

   
       
//6.判断一个文件是否可删除
        result3 = [fm
isDeletableFileAtPath:@"/Users/mac/Desktop/1.txt"];

        if
(result3) {

            NSLog(@"文件可删除");
        }else{NSLog(@"文件不可删除");}

// 
获取子目录
       
//获取属性

        NSDictionary
*dict =  [fm
attributesOfItemAtPath:@"/Users/mac/Desktop/1.txt"
error:nil];           //字典接收

        NSLog(@"%@",dict);

          //获取创建时间

        NSLog(@"%@",dict[@"NSFileModificationDate"]);
  //键值对的概念

   

     //1.按照递归方式获取子目录-----递归的方式是遍历到没有文件

          NSArray
*arr =  [fm
subpathsAtPath:@"/Users/mac/Desktop/共享视频"];

          NSLog(@"%lu",arr.count);

          NSLog(@"%@",arr);
   
//2.按照非递归方式获取子目录---只遍历一层
       
NSArray *arr1= [fm
contentsOfDirectoryAtPath:
@"/Users/mac/Desktop/oc讲义"error:nil];

        NSLog(@"%lu",arr1.count);
       
NSLog(@"%@",arr1);

    //文件管理器的创建
       
NSFileManager  *fm = [NSFileManagerdefaultManager];
//创建文件
        NSString *str =
@"披星戴月,的,奔波,只为,那扇窗";

        //将字符串转化为NSdata类型,转化为二进制,NSData存储的是二进制

        NSData *da = [str
dataUsingEncoding:NSUTF8StringEncoding];

        BOOL  result  = [fm
createFileAtPath:@"/Users/mac/Desktop/aaa.txt"contents:da
attributes:nil];

        if(result){NSLog(@"文件创建成功");}

        else{NSLog(@"文件没有创建成功");}
//创建目录

        //参数2的意思是:yes和no,是否要创建中间目录

        result = [fm createDirectoryAtPath:@"/Users/mac/Desktop/bbb/"withIntermediateDirectories:YESattributes:nilerror:nil];

        if(result){NSLog(@"目录创建成功");}

        else{NSLog(@"目录没有创建成功");}

//拷贝文件bbb

        //参数1是:要拷贝的文件   
参数2是拷贝到哪

    result =  [fm copyItemAtPath:@"/Users/mac/Desktop/aaa.txt"toPath:@"/Users/mac/Desktop/bbb.txt"error:nil];

        if(result){NSLog(@"文件拷贝成功");}

        else{NSLog(@"文件没有拷贝成功");}
//移动文件

      //参数1是:要移动的文件   
参数2是移动到哪

        result =  [fm moveItemAtPath:@"/Users/mac/Desktop/aaa.txt"toPath:@"/Users/mac/Desktop/11.txt"error:nil];

        if(result){NSLog(@"文件移动成功");}

            else{NSLog(@"文件没有移动成功");}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: