Objective-C( Foundation框架 一 NSFileManager)

NSFileManager

用来管理文件系统的

它可以用于常见的文件,文件夹操作(拷贝,剪切,创建)

NSFileManager使用了单例模式(Singleton)

使用defaultManager可以获得那个单例对象:[NSFileManager defaultManager];

 

NSFileManager用于判断

// 创建一个文件,且内容为hahdhahkf
        NSString *arr = @"hahdhahkf";
        [arr writeToFile: @"/Users/cloudwalk/Desktop/arr.plist" atomically:YES encoding:NSUTF8StringEncoding error:nil];
        // NSFileManager用于判断
        NSString *filepath = @"/Users/cloudwalk/Desktop/arr.plist";
        // 判断文件是否存在
        // 调用defaultManagr 创建一个文件管理的单例模式
        // 单例模式,在程序运行期间,只有一个对象存在
        NSFileManager *manager = [NSFileManager defaultManager];
        //文件是否存在,存在返回1,不存在返回0
 BOOL isYES = [manager fileExistsAtPath:filepath]; 
NSLog(
@"%d",isYES); // 文件是否可读
isYES = [manager isReadableFileAtPath:filepath];
NSLog(
@"%d",isYES); // 文件是否可写
isYES = [manager isWritableFileAtPath:filepath];
NSLog(
@"%d",isYES);

判断是否是目录

        NSString *str = @"dsahgifg";
        [str writeToFile:@"/Users/cloudwalk/Desktop/strr,plist" atomically:YES encoding:NSUTF8StringEncoding error:nil];
        NSString *filePath = @"/Users/cloudwalk/Desktop/strr,plist";
        
        NSFileManager *manager = [NSFileManager defaultManager];
        // 文件是否存在,,存在返回1,不存在返回0
        BOOL isYes = [manager fileExistsAtPath:filePath];
        NSLog(@"%d",isYes);
        
        if (isYes)
        {
            BOOL isDir;
            // 判断是不是目录
            [manager fileExistsAtPath:filePath isDirectory:&isDir];
            
            if (isDir) {
                NSLog(@"shi");
            }else{
                NSLog(@"bushi");
            }
        }

 

获取文件信息

        NSFileManager *fm = [NSFileManager defaultManager];
        NSString *filepath = @"/Users/cloudwalk/Desktop/test.plist";
        NSString *dirPath = @"/Users/cloudwalk/Desktop/gaoli/代码/";
        // 如何获得文件信息(属性)
        NSDictionary *dy = [fm attributesOfItemAtPath:filepath error:nil];
        NSLog(@"%@",dy);
        NSLog(@"%@ %@",[dy objectForKey:@"NSFileOwnerAccountName"], dy[@"NSFileOwnerAccountName"]);
        
        // 获取指定目录下文件及子目录
        // 使用递归的方式,获取当前所有文件夹及子目录下的文件
       NSArray *subPaths = [fm subpathsAtPath:dirPath];
        NSLog(@"subPaths = %@",subPaths);
        // 不使用递归的方式,获取当前所有文件夹及子目录下的文件
        subPaths = [fm subpathsOfDirectoryAtPath:dirPath error:nil];
        NSLog(@"subPaths = %@",subPaths);
        // 获取指定目录下文件信息(不再获取子目录下文件)
        subPaths = [fm contentsOfDirectoryAtPath:dirPath error:nil];
        NSLog(@"subPaths = %@",subPaths);

 

创建目录,文件,copy文件,移动文件,删除文件

       // 创建文件管理对象
        NSFileManager *fm = [NSFileManager defaultManager];
        
        // 创建路径
        NSString *createPath = @"/Users/cloudwalk/Desktop/aaa/win.txt";
        
        // 创建目录是否成功
        //fm createDirectoryAtPath:@"路径" withIntermediateDirectories:YES会创建缺失的目录/NO不会创建缺失的目录 attributes:nil error:错误对象
        BOOL isYES =[fm createDirectoryAtPath:createPath withIntermediateDirectories:YES attributes:nil error:nil];
        if (isYES) {
            NSLog(@"成功");
        }
        
        // 如何创建文件

      NSString *str = @"我一定会考进HEIMA";

      NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];

        BOOL isYes;
         //fm createFileAtPath:@"路径" contents:NSData类型的数据/处理二进制数据的类 attributes:文件的属性的字典NSDictionary
        isYes = [fm createFileAtPath:createPath contents:data attributes:nil];
        if (isYes) {
            NSLog(@"%d",isYes);
        }
        // copy文件
        NSString *targetPath = @"/Users/cloudwalk/Desktop/gaoli/haha.txt";
        isYes = [fm copyItemAtPath:createPath toPath:targetPath error:nil];
        NSLog(@"%d",isYes);
        
        // 移动文件
        NSString *targetPath = @"/Users/cloudwalk/Desktop/haha.txt";
        [fm moveItemAtPath:createPath toPath:targetPath error:nil];
        
        // 删除文件
        NSString *targetPath = @"/Users/cloudwalk/Desktop/haha.txt";
        [fm removeItemAtPath:targetPath error:nil];

 

NSFileManager 文件下载思路

1.发送请求给服务器,要求下载某个文件

2.服务器发出响应,返回文件数据

3.手机客户端利用NSData来存放服务器返回的文件数据

4.利用NSFileManager将NSData里面的文件数据写到新的文件中(createFilecreateFileAtPath)

 

转载于:https://www.cnblogs.com/1023843587qq/p/4801265.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值