NSFileManager的基本使用 - OC

  • 什么是NSFileManager?
    NSFileManager是用來管理文件系統(tǒng)的
    它可以用來進(jìn)行常見的文件\文件夾操作(拷貝、剪切、創(chuàng)建等)

  • NSFileManager使用了單例模式singleton
    使用defaultManager方法可以獲得那個(gè)單例對象
    [NSFileManager defaultManager]

    //NSFileManager 用于判斷
    
    NSString *filePath = @"/Users/zhaoxiaohu/Desktop/arr.plist";
    
     NSString *filePath2 = @"/";
1) 判斷文件是否存在
    //創(chuàng)建文件管理對象
    //調(diào)用defaultManager 創(chuàng)建一個(gè)文件管理的單例對象
    //單例對象:在程序運(yùn)行期間,只有一個(gè)對象存在
    NSFileManager *fm = [NSFileManager defaultManager];
    // YES 存在   NO 不存在
    BOOL isYES = [fm fileExistsAtPath:filePath];
    NSLog(@"-->%d",isYES);
2) 判斷是否是一個(gè)目錄
    if(isYES){
        BOOL isDir;
    
        // 2) 判斷是否是一個(gè)目錄
        [fm fileExistsAtPath:filePath isDirectory:&isDir];
        
        if (isDir) {
            NSLog(@"這是一個(gè)目錄");
        }else{
        
            NSLog(@"這不是一個(gè)目錄");
        }
    
    }
3) 判斷文件是否可讀
    isYES = [fm isReadableFileAtPath:filePath];
4) 是否可寫
    isYES = [fm isWritableFileAtPath:filePath2];
5) 是否可刪除
    isYES = [fm isDeletableFileAtPath:filePath2];
    
             NSLog(@"-->%d",isYES);
6)獲取文件的信息(屬性)
    //創(chuàng)建文件對象
    NSFileManager *fm = [NSFileManager defaultManager];
    NSString *filePath = @"/Users/zhaoxiaohu/Desktop/arr.plist";
    
    NSString *dirPath = @"/Users/zhaoxiaohu/Desktop/a";
   
    //1)如何獲取文件的信息(屬性)
    NSDictionary *dict = [fm attributesOfItemAtPath:filePath error:nil];
    NSLog(@"%@",dict);
    NSLog(@"%@,%@",[dict objectForKey:@"NSFileOwnerAccountName"],dict[@"NSFileOwnerAccountName"]);
7)獲取指定目錄下文件及子目錄
    //使用遞歸的方式 獲取當(dāng)前目錄及子目錄下的所有的文件及文件夾
    NSArray *subPaths = [fm subpathsAtPath:dirPath];
    
    //(推薦使用)subpathsOfDirectoryAtPath 不是使用遞歸的方式獲取的
    subPaths = [fm subpathsOfDirectoryAtPath:dirPath error:nil];
    NSLog(@"subPaths = %@",subPaths);
8)獲取指定目錄下的文件及目錄信息(不在獲取后代路徑)
    subPaths = [fm contentsOfDirectoryAtPath:dirPath error:nil];
    NSLog(@"subPaths = %@",subPaths);
9)創(chuàng)建目錄
   //創(chuàng)建文件管理對象
    NSFileManager *fm = [NSFileManager defaultManager];
    
    //如何創(chuàng)建目錄 (路徑  :/Users/zhaoxiaohu/Desktop/aaa)
    NSString *createDirPath = @"/Users/zhaoxiaohu/Desktop/aaa/ccc/bbb/love.txt";
    fm createDirectoryAtPath:@"路徑" withIntermediateDirectories:YES/NO 創(chuàng)建路徑的時(shí)候,YES自動(dòng)創(chuàng)建路徑中缺少的目錄,NO的不會(huì)創(chuàng)建缺少的目錄 attributes:屬性的字典 error:錯(cuò)誤對象
    
           BOOL isYES = [fm createDirectoryAtPath:createDirPath withIntermediateDirectories:YES attributes:nil error:nil];
   
    if (isYES) {
        NSLog(@"成功");
   }
10)創(chuàng)建文件
    //如何創(chuàng)建文件
    NSString *str = @"每當(dāng)我錯(cuò)過一個(gè)女孩,我就向山上放一塊磚,于是就有了長城";
    //writeToFile
    //fm createFileAtPath:@"路徑" contents: NSData類型的數(shù)據(jù) attributes:文件的屬性的字典
    //創(chuàng)建NSData?   是一個(gè)處理二進(jìn)制數(shù)據(jù)的類
    //NSString -----> NSData  (把NSString轉(zhuǎn)化成NSData)
    
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    BOOL isYes;
    // createFileAtPath 創(chuàng)建文件
    isYes = [fm createFileAtPath:createDirPath contents:data attributes:nil];
    NSLog(@"isYes = %d",isYes);
11)copy文件
    //如何copy文件
    NSString *targetPath = @"/Users/zhaoxiaohu/Desktop/aaa/ccc/love.txt";
    [fm copyItemAtPath:createDirPath toPath:targetPath error:nil];
    
     NSString *targetPath = @"/Users/zhaoxiaohu/Desktop/aaa/love.txt";
12)移動(dòng)文件
    //如何移動(dòng)文件
    [fm moveItemAtPath:createDirPath toPath:targetPath error:nil];
13)刪除文件
    //如何刪除文件
    [fm removeItemAtPath:targetPath error:nil];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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