iOS中NSFileManager的使用

来源:互联网 发布:淘宝优惠券赚钱 编辑:程序博客网 时间:2024/06/01 08:43


   //NSFileManager用于判断

        NSString * filePath=@"/Users/ms/Desktop/contact.json";

        NSString * filePath1=@"/Users/ms/Desktop";

        //1.文件是否存在

        NSFileManager * fm=[NSFileManagerdefaultManager];

        //调用defaultManager,创建一个文件管理的单例对象

       BOOL isYES= [fm fileExistsAtPath:filePath];

        NSLog(@"%d",isYES);

        //2.文件是否是一个目录

        BOOL isDir;

        [fm fileExistsAtPath:filePath1 isDirectory:&isDir];

        if (isDir) {

            NSLog(@"这是一个目录");

        }else

        {

            NSLog(@"这不是一个目录");

        }

        //3.文件是否可读

        isYES=[fm isReadableFileAtPath:filePath];

        NSLog(@"%d",isYES);

        //4.文件是否可写

        isYES=[fm isWritableFileAtPath:filePath];

        NSLog(@"%d",isYES);

        //5.文件是否可删除

        isYES=[fm isDeletableFileAtPath:filePath];

        NSLog(@"%d",isYES);

===============================================================

  //创建文件对象

    NSFileManager * fm=[NSFileManagerdefaultManager];

    NSString * path=@"/Users/ms/Desktop/contact.json";

        NSString * path1=@"/Users/ms/Desktop";

      //1如何获取文件属性

    NSDictionary * dict=[fmattributesOfItemAtPath:path error:nil];

    NSLog(@"%@",dict);

        NSLog(@"%@",dict[@"NSFileType"]);

    //2获取指定目录下的子目录

        //使用递归的方式获取当前目录及子目录下的所有的文件和文件夹

       NSArray * subPath= [fm subpathsAtPath:path1];

       // NSLog(@"subPath=%@",subPath);

        

    subPath=[fm subpathsOfDirectoryAtPath:path1error:nil];

        //这种方式不用递归的方式获取,递归压栈,费时间和内存

        //NSLog(@"subPath=%@",subPath);

        //3获取指定目录下的文件及文件夹

     subPath= [fm contentsOfDirectoryAtPath:path1error:nil];

     NSLog(@"subPath=%@",subPath);

    }

===========================================================

  //创建文件管理对象

        NSFileManager * fm=[NSFileManagerdefaultManager];

        //如何创建目录(/Users/ms/Desktop/aaa)

        NSString * createPath=@"/Users/ms/Desktop/aaa";

      //  fm createDirectoryAtPath:@"" withIntermediateDirectories:@"如果aaa前面没有desktop是否创建aaa文件前面文件" attributes:@"属性" error:@"错误对象"

        

        BOOL isYES=[fmcreateDirectoryAtPath:createPath withIntermediateDirectories:NOattributes:nilerror:nil];

        if (isYES) {

            NSLog(@"成功");

        }

        //如何创建文件

         NSString * createPath=@"/Users/ms/Desktop/aaa/love.txt";

        NSString * str=@"每当我错过一个女孩,我就向山上放一块砖,于是就有了长城";

       // fm createFileAtPath:@"路径" contents:NSdata类型的数据 attributes:文件的属性的字典

        //创建NSdata用于处理二进制数据的类

        NSData * data=[strdataUsingEncoding:NSUTF8StringEncoding];

        BOOL isYES=[fm createFileAtPath:createPath contents:data attributes:nil];

        if (isYES) {

            NSLog(@"%d",isYES);

        }

        //如何copy文件

    NSString * createPath=@"/Users/ms/Desktop/aaa/love.txt";

    NSString * createPath1=@"/Users/ms/Desktop/love.txt";

    //[fm copyItemAtPath:createPath toPath:createPath1 error:nil];

        //如何移动文件

        [fm moveItemAtPath:createPath toPath:createPath1 error:nil];

        //如何删除文件

        [fm removeItemAtPath:createPath1 error:nil];


0 0
原创粉丝点击