sandBox 文件管理器

文件管理器侧重于文件的操作,我们可以实现文件的创建,文件的移动(剪切),文件的复制。


        // 1.文件创建
        // 拼接文件路径
        NSString *filePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
        filePath = [filePath stringByAppendingPathComponent:@"createFile.txt"];
        // 判断文件是否存在
        BOOL isExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
        NSLog(@"%@",filePath);
    
        if (isExists) {
    
            NSLog(@"已经存在");
        }else {
    
            NSString *content = @"我有一对象";
            // 创建文件
            BOOL isCreate = [[NSFileManager defaultManager] createFileAtPath:filePath contents:[content dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
            if (isCreate) {
    
                NSLog(@"创建成功");
            }else {
    
                NSLog(@"创建失败");
            }
        }
        // 2、文件的剪切
        // 注意点:文件的剪切和赋值需要注意,剪切和复制的最终路径不能存在文件(路径上面只能是空的)跨文件夹的话,文件夹一定得先存在,才能移动复制成功
        // 拼接文件路径
        // 相当于获取的时候Documents文件夹的路径
        NSString *lastPath = [filePath stringByDeletingLastPathComponent];
        // 拼接一个新的文件夹路径
        NSString *newPath = [lastPath stringByAppendingPathComponent:@"Move"];
        // 1、创建文件夹
        if ([[NSFileManager defaultManager] fileExistsAtPath:newPath]) {
            // 当文件夹存在的时候需要干的事
            NSLog(@"文件夹已经存在");
            // 拼接文件路径
            NSString *movePath = [newPath stringByAppendingPathComponent:@"createFile.txt"];
    //        // 移动
//            BOOL isMoving = [[NSFileManager defaultManager] moveItemAtPath:filePath toPath:movePath error:nil];
//            if (isMoving) {
//    
//                NSLog(@"移动成功");
//            } else {
//    
//                NSLog(@"移动失败");
//            }
//             复制
            BOOL isCopying = [[NSFileManager defaultManager]copyItemAtPath:filePath toPath:movePath error:nil];
            if (isCopying) {
    
                NSLog(@"复制成功");
            } else {
    
                NSLog(@"复制失败");
            }
        }else {
    
            // 当文件夹不存时候需要做的事
            // 创建文件夹
            // YES同上
             BOOL isCreating = [[NSFileManager defaultManager]createDirectoryAtPath:newPath withIntermediateDirectories:YES attributes:nil error:nil];
            if (isCreating) {
    
                NSLog(@"创建成功");
                // 拼接文件路径
                NSString *movePath = [newPath stringByAppendingPathComponent:@"createFile.txt"];
    //            // 移动
    //            BOOL isMoving = [[NSFileManager defaultManager] moveItemAtPath:filePath toPath:movePath error:nil];
    //            if (isMoving) {
    //
    //                NSLog(@"移动成功");
    //            } else {
    //
    //                NSLog(@"移动失败");
    //            }
                // 复制
                BOOL isCopying = [[NSFileManager defaultManager]copyItemAtPath:filePath toPath:movePath error:nil];
                if (isCopying) {
    
                    NSLog(@"复制成功");
                } else {
    
                    NSLog(@"复制失败");
                }
    
            } else {
    
                NSLog(@"创建失败");
            }
        }
    
    
    
}

![屏幕快照 2016-06-24 下午9.43.23.png](http://upload-images.jianshu.io/upload_images/2160604-62bf1f9b53a9d866.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音。了解发音是有意...
    萤火虫de梦阅读 99,628评论 9 467
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,026评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,687评论 25 708
  • 你脸上纵横的沟壑 笑起来尤为明显 那就笑着好了 即便镌刻了岁月的明显痕迹 也有被冲刷出的恬淡 佝偻的大山啊 夕阳下...
    苏格拉风掠影阅读 518评论 52 54
  • 还记得我们年少的梦想吗? 年少的我们内心总那么轻狂,曾想四海为家得你 今又在何方? 曾梦想仗剑走天涯,...
    旅游歆怡阅读 195评论 0 1