美文网首页
sandBox 文件管理器

sandBox 文件管理器

作者: 我爱吃豆芽 | 来源:发表于2016-06-24 21:44 被阅读44次

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


        // 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](https://img.haomeiwen.com/i2160604/62bf1f9b53a9d866.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

相关文章

  • sandBox 文件管理器

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

  • android webview 文件上传

    1、唤出系统文件管理器 开启文件上传,可使用HTML5标签 唤出系统文件管理器或自定义文件管理器,然后选择文件...

  • iOS 文件操作简介

    级别: ★★☆☆☆标签:「iOS 文件操作」「SandBox」「QiFileManager」作者: dac_103...

  • 02、文件管理器、文件对接器与复杂对象的读写操作

    一、文件管理器与文件连接器 1、文件管理器(NSFileManger),主要是对文件进行创建,删除,改名以及文件信...

  • Mac HBuilder 快捷键

    项目管理器 重命名文件(焦点在项目管理器中):f2剪切文件(焦点在项目管理器中):command+x复制文件(焦点...

  • 沙盒 文件

    沙盒SandBox 向沙盒写文件和读文件 文件管理NSFileManager 图片下载 创建文件夹 创建文件 复制...

  • macOS sandbox 文件权限

    apple 在这个地方限制的很死 下面是设置 app 可以打开 Terminal, 并且有 hosts 文件的读写...

  • iOS开发中数据持久化的总结

    数据持久化链接导航: 沙盒基本机制(sandbox) 数据持久化文件读写(plist 文件)NSUserDefau...

  • 基础:解压皮肤

    返回使用指南 软件:es文件管理器 不要拘泥,基本上所有的文件管理器都可以支持。例如mt管理器

  • 一加五删除系统预装软件

    条件: 手机已ROOT 工具:RE文件管理器(教程已此为示例)、MT管理器,其他支持修改系统文件的管理器就可以 ...

网友评论

      本文标题:sandBox 文件管理器

      本文链接:https://www.haomeiwen.com/subject/grzodttx.html