sandBox 文件管理器

文件管理器側(cè)重于文件的操作,我們可以實(shí)現(xiàn)文件的創(chuàng)建,文件的移動(dòng)(剪切),文件的復(fù)制。


        // 1.文件創(chuàng)建
        // 拼接文件路徑
        NSString *filePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
        filePath = [filePath stringByAppendingPathComponent:@"createFile.txt"];
        // 判斷文件是否存在
        BOOL isExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
        NSLog(@"%@",filePath);
    
        if (isExists) {
    
            NSLog(@"已經(jīng)存在");
        }else {
    
            NSString *content = @"我有一對(duì)象";
            // 創(chuàng)建文件
            BOOL isCreate = [[NSFileManager defaultManager] createFileAtPath:filePath contents:[content dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
            if (isCreate) {
    
                NSLog(@"創(chuàng)建成功");
            }else {
    
                NSLog(@"創(chuàng)建失敗");
            }
        }
        // 2、文件的剪切
        // 注意點(diǎn):文件的剪切和賦值需要注意,剪切和復(fù)制的最終路徑不能存在文件(路徑上面只能是空的)跨文件夾的話,文件夾一定得先存在,才能移動(dòng)復(fù)制成功
        // 拼接文件路徑
        // 相當(dāng)于獲取的時(shí)候Documents文件夾的路徑
        NSString *lastPath = [filePath stringByDeletingLastPathComponent];
        // 拼接一個(gè)新的文件夾路徑
        NSString *newPath = [lastPath stringByAppendingPathComponent:@"Move"];
        // 1、創(chuàng)建文件夾
        if ([[NSFileManager defaultManager] fileExistsAtPath:newPath]) {
            // 當(dāng)文件夾存在的時(shí)候需要干的事
            NSLog(@"文件夾已經(jīng)存在");
            // 拼接文件路徑
            NSString *movePath = [newPath stringByAppendingPathComponent:@"createFile.txt"];
    //        // 移動(dòng)
//            BOOL isMoving = [[NSFileManager defaultManager] moveItemAtPath:filePath toPath:movePath error:nil];
//            if (isMoving) {
//    
//                NSLog(@"移動(dòng)成功");
//            } else {
//    
//                NSLog(@"移動(dòng)失敗");
//            }
//             復(fù)制
            BOOL isCopying = [[NSFileManager defaultManager]copyItemAtPath:filePath toPath:movePath error:nil];
            if (isCopying) {
    
                NSLog(@"復(fù)制成功");
            } else {
    
                NSLog(@"復(fù)制失敗");
            }
        }else {
    
            // 當(dāng)文件夾不存時(shí)候需要做的事
            // 創(chuàng)建文件夾
            // YES同上
             BOOL isCreating = [[NSFileManager defaultManager]createDirectoryAtPath:newPath withIntermediateDirectories:YES attributes:nil error:nil];
            if (isCreating) {
    
                NSLog(@"創(chuàng)建成功");
                // 拼接文件路徑
                NSString *movePath = [newPath stringByAppendingPathComponent:@"createFile.txt"];
    //            // 移動(dòng)
    //            BOOL isMoving = [[NSFileManager defaultManager] moveItemAtPath:filePath toPath:movePath error:nil];
    //            if (isMoving) {
    //
    //                NSLog(@"移動(dòng)成功");
    //            } else {
    //
    //                NSLog(@"移動(dòng)失敗");
    //            }
                // 復(fù)制
                BOOL isCopying = [[NSFileManager defaultManager]copyItemAtPath:filePath toPath:movePath error:nil];
                if (isCopying) {
    
                    NSLog(@"復(fù)制成功");
                } else {
    
                    NSLog(@"復(fù)制失敗");
                }
    
            } else {
    
                NSLog(@"創(chuàng)建失敗");
            }
        }
    
    
    
}

![屏幕快照 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)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Ubuntu的發(fā)音 Ubuntu,源于非洲祖魯人和科薩人的語言,發(fā)作 oo-boon-too 的音。了解發(fā)音是有意...
    螢火蟲de夢(mèng)閱讀 100,814評(píng)論 9 468
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,694評(píng)論 19 139
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,323評(píng)論 25 708
  • 你臉上縱橫的溝壑 笑起來尤為明顯 那就笑著好了 即便鐫刻了歲月的明顯痕跡 也有被沖刷出的恬淡 佝僂的大山啊 夕陽下...
    蘇格拉風(fēng)掠影閱讀 545評(píng)論 52 54
  • 還記得我們年少的夢(mèng)想嗎? 年少的我們內(nèi)心總那么輕狂,曾想四海為家得你 今又在何方? 曾夢(mèng)想仗劍走天涯,...
    旅游歆怡閱讀 211評(píng)論 0 1

友情鏈接更多精彩內(nèi)容