NSFileManager

本文介绍了如何利用NSFileManager进行文件管理操作,包括获取文件信息、创建目录、复制、移动、删除文件等,并提供了详细的Objective-C代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


//创建文件管理对象
        NSFileManager *fm = [NSFileManager defaultManager];
        
        //1、获取文件的信息
        NSString *filePath = @"/Users/apple/Desktop/arr.plist";
        //我要获取 文件的信息(大小、创建时间、修改时间、权限.....)
        
        NSError *err;
        //获取文件的属性 attributesOfItemAtPath
        NSDictionary *fileAttr = [fm attributesOfItemAtPath:filePath error:&err];
        
        NSLog(@"fileAttr = %@",fileAttr);
        
        // 2、获取目录子路径
        //subpathsAtPath  获取当前目录及所有子目录下文件路径
        filePath = @"/Users/apple/Desktop/test";
//        NSArray *paths = [fm subpathsAtPath:filePath];
//        NSLog(@"paths.count = %ld",paths.count);
        
        //subpathsOfDirectoryAtPath 获取当前目录及所有子目录下文件路径
        
//        NSArray *paths =[fm subpathsOfDirectoryAtPath:filePath error:&err];
//        NSLog(@"path = %@",paths);
        
        // 3、获取当前文件夹下地目录(不包括子目录)
        NSArray *paths =[fm contentsOfDirectoryAtPath:filePath error:&err];
        
        NSLog(@"path = %@",paths);



//创建文件管理对象
        NSFileManager *fm = [NSFileManager defaultManager];
        
        NSString *filePath = @"/Users/apple/Desktop/itcast/aaa/bbb";
        //创建目录
//        [fm createDirectoryAtPath:路径 withIntermediateDirectories:YES/NO --- YES表示创建子文件夹 attributes:属性的字典 error:错误信息]
        NSError *err = nil;
        //创建目录
//        [fm createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:&err];
//        
//        
//        if (err==nil) {
//            NSLog(@"创建成功");
//        }else{
//        
//            NSLog(@"err = %@",err);
//        }
        
        //复制文件
        // itcast/aaa/bbb/1.txt
        NSString *oldFilePath = @"/Users/apple/Desktop/itcast/aaa/bbb/1.txt";
        // itcast/aaa/1.txt
        NSString *goalFilePath =@"/Users/apple/Desktop/itcast/aaa/1.txt";
//        //复制文件
//        [fm copyItemAtPath:oldFilePath toPath:goalFilePath error:&err];
//        
//        if (err==nil) {
//            NSLog(@"复制成功");
//        }else{
//            
//            NSLog(@"err = %@",err);
//        }
        
        
        //移动文件(剪切)
//        [fm moveItemAtPath:oldFilePath toPath:goalFilePath error:&err];
//        
//        if (err==nil) {
//            NSLog(@"移动成功");
//        }else{
//
//            NSLog(@"err = %@",err);
//        }
        //删除文件
        
//        [fm removeItemAtPath:goalFilePath error:&err];
//        if (err==nil) {
//            NSLog(@"删除成功");
//        }else{
//
//            NSLog(@"err = %@",err);
//        }
        
        
        //创建文件
        NSString *str = @"见于不见,丈母娘就在那里,不躲不闪";
        //NSData   文件操作
        //输入输出流
        NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
        
        //创建文件
        if([fm createFileAtPath:goalFilePath contents:data attributes:nil]){
        
            NSLog(@"文件创建成功");
        
        }
        
        //删除目录
        oldFilePath = @"/Users/apple/Desktop/itcast/aaa/";

        [fm removeItemAtPath:oldFilePath error:&err];






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值