intmain(intargc,constchar*argv[]){@autoreleasepool{//創(chuàng)建文件管理對(duì)象NSFileManager*fm=[NSFileManagerdefaultManager];//要操作的文件名NSString*fname=@"myfile";//獲取文件的字典N(xiāo)SDictionary*attr;//當(dāng)前路徑N" />

欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

Foundation 框架 NSFileManager,NSData 簡(jiǎn)單的

系統(tǒng) 1808 0

一、簡(jiǎn)單展示NSFileManager的使用

      
        #import
      
       <Foundation/Foundation.h>




      
        int
      
       main(
      
        int
      
       argc, 
      
        const
      
      
        char
      
       *
      
         argv[])

{



    @autoreleasepool {

        
      
      
        //
      
      
        創(chuàng)建文件管理對(duì)象
      
      

        NSFileManager *fm =
      
         [NSFileManager defaultManager];

        
      
      
        //
      
      
        要操作的文件名
      
      

        NSString *fname = 
      
        @"
      
      
        myfile
      
      
        "
      
      
        ;

        
      
      
        //
      
      
        獲取文件的字典
      
      

        NSDictionary *
      
        attr;

        
      
      
        //
      
      
        當(dāng)前路徑
      
      

        NSString *
      
        path;

        
      
      
        //
      
      
        獲取當(dāng)前路徑
      
      

        path =
      
         [fm currentDirectoryPath];

        
      
      
        //
      
      
        NSLog(@"\nThe current path is : %@", path);

        

        
      
      
        //
      
      
        檢測(cè)文件是否存在
      
      
        if
      
       ([fm fileExistsAtPath: fname] ==
      
         NO) {

            
      
      
        //
      
      
        如果不存在則建立一個(gè)文件
      
      
                    [fm createFileAtPath: fname contents: NULL attributes:nil];

            
      
      
        //
      
      
        NSLog(@"\nThe file is not exist!");

            
      
      
        //
      
      
        return 0;
      
      
                }

        
      
      
        //
      
      
        拷貝創(chuàng)建一個(gè)新文件, 新文件若已存在則報(bào)錯(cuò)
      
      
        if
      
       ([fm copyItemAtPath: fname toPath: 
      
        @"
      
      
        newFile
      
      
        "
      
       error: NULL] ==
      
         NO) {

            NSLog(
      
      
        @"
      
      
        \n Can't copy the file
      
      
        "
      
      
        );

            
      
      
        return
      
      
        1
      
      
        ;

        }

        
      
      
        //
      
      
        檢測(cè)兩個(gè)文件內(nèi)容是否相同
      
      
        if
      
       ([fm contentsEqualAtPath: fname andPath: 
      
        @"
      
      
        newFile
      
      
        "
      
      ] ==
      
         NO) {

            NSLog(
      
      
        @"
      
      
        \nThe contents is not same
      
      
        "
      
      
        );

            
      
      
        return
      
      
        2
      
      
        ;

        }

        
      
      
        //
      
      
        移動(dòng)或者改名文件
      
      
        if
      
       ([fm moveItemAtPath: 
      
        @"
      
      
        newFile
      
      
        "
      
        toPath: 
      
        @"
      
      
        myFile2
      
      
        "
      
       error:NULL] ==
      
         NO) {

            NSLog(
      
      
        @"
      
      
        \nCan't change the name
      
      
        "
      
      
        );

            
      
      
        return
      
      
        3
      
      
        ;

        }

        
      
      
        //
      
      
        獲取文件數(shù)據(jù)字典
      
      
        if
      
       ((attr = [fm attributesOfItemAtPath: fname error:NULL]) ==
      
         nil) {

            NSLog(
      
      
        @"
      
      
        \nGet attributets failed
      
      
        "
      
      
        );

            
      
      
        return
      
      
        4
      
      
        ;

        }

        
      
      
        //
      
      
        文件大小
      
      

        NSLog(
      
        @"
      
      
        %@
      
      
        "
      
      
        , attr[NSFileSize]);

        
      
      
        //
      
      
        文件類型
      
      

        NSLog(
      
        @"
      
      
        %@
      
      
        "
      
      
        , attr[NSFileType]);

        
      
      
        //
      
      
        創(chuàng)建者
      
      

        NSLog(
      
        @"
      
      
        %@
      
      
        "
      
      
        , attr[NSFileOwnerAccountName]);

        
      
      
        //


      
              NSLog(
      
        @"
      
      
        %@
      
      
        "
      
      
        , attr[NSFileCreationDate]);

        
      
      
        //
      
      
        顯示文件內(nèi)容
      
      

        NSLog(
      
        @"
      
      
        \n Show the file contents
      
      
        "
      
      
        );

        NSLog(
      
      
        @"
      
      
        \n%@
      
      
        "
      
      
        , [NSString stringWithContentsOfFile: fname encoding:NSUTF8StringEncoding error:NULL]);

    }

    
      
      
        return
      
      
        0
      
      
        ;

}
      
    

?二、通過(guò)NSData完成副本制作

      
         1
      
      
        int
      
       main(
      
        int
      
       argc, 
      
        const
      
      
        char
      
       *
      
         argv[])


      
      
         2
      
      
        {


      
      
         3
      
      
         4
      
      
            @autoreleasepool {


      
      
         5
      
      
        //
      
      
        通過(guò)NSDate來(lái)完成文件副本制作
      
      
         6
      
               NSFileManager *fm =
      
         [NSFileManager defaultManager];


      
      
         7
      
               NSData *
      
        dt;


      
      
         8
      
      
         9
      
               dt = [fm contentsAtPath: 
      
        @"
      
      
        myfile
      
      
        "
      
      
        ];


      
      
        10
      
      
        11
      
      
        if
      
       (dt ==
      
         nil) {


      
      
        12
      
                   NSLog(
      
        @"
      
      
        Read file failed....
      
      
        "
      
      
        );


      
      
        13
      
      
        return
      
      
        0
      
      
        ;


      
      
        14
      
      
                }


      
      
        15
      
      
        16
      
      
        //
      
      
        將緩沖區(qū)NSData中的內(nèi)容復(fù)制到文件中
      
      
        17
      
      
        if
      
       ([fm createFileAtPath:
      
        @"
      
      
        myFavoriteFile
      
      
        "
      
       contents: dt attributes:nil] ==
      
         NO) {


      
      
        18
      
                   NSLog(
      
        @"
      
      
        Creat backups failed
      
      
        "
      
      
        );


      
      
        19
      
      
        return
      
      
        1
      
      
        ;


      
      
        20
      
      
                }


      
      
        21
      
      
        22
      
      
        //
      
      
        讀出文件內(nèi)容
      
      
        23
      
               NSLog(
      
        @"
      
      
        \n%@
      
      
        "
      
      , [NSString stringWithContentsOfFile:
      
        @"
      
      
        myFavoriteFile
      
      
        "
      
      
         encoding: NSUTF8StringEncoding error:NULL]);


      
      
        24
      
      
            }


      
      
        25
      
      
        return
      
      
        0
      
      
        ;


      
      
        26
      
       }
    

三、簡(jiǎn)單的目錄操作

      
         1
      
      
        #import
      
       <Foundation/Foundation.h>


      
         2
      
      
         3
      
      
        int
      
       main(
      
        int
      
       argc, 
      
        const
      
      
        char
      
       *
      
         argv[])


      
      
         4
      
      
        {


      
      
         5
      
      
         6
      
      
            @autoreleasepool {


      
      
         7
      
               NSString *newDir = 
      
        @"
      
      
        newDir
      
      
        "
      
      
        ;


      
      
         8
      
               NSString *
      
        currentPath;


      
      
         9
      
               NSFileManager *fm =
      
         [NSFileManager defaultManager];


      
      
        10
      
      
        11
      
      
        //
      
      
        獲取當(dāng)前路徑
      
      
        12
      
               currentPath =
      
         [fm currentDirectoryPath];


      
      
        13
      
               NSLog(
      
        @"
      
      
        \nCurrentpath is : \n%@
      
      
        "
      
      
        , currentPath);


      
      
        14
      
      
        15
      
      
        //
      
      
        在當(dāng)前目錄下新建一個(gè)目錄
      
      
        16
      
      
        if
      
       ([fm createDirectoryAtPath:newDir withIntermediateDirectories:TRUE attributes:nil error:NULL] ==
      
         NO) {


      
      
        17
      
                   NSLog(
      
        @"
      
      
        \nCouldn't creat the directory...
      
      
        "
      
      
        );


      
      
        18
      
      
        return
      
      
        0
      
      
        ;


      
      
        19
      
      
                }


      
      
        20
      
      
        21
      
      
        //
      
      
        更改路徑名
      
      
        22
      
      
        if
      
       ([fm moveItemAtPath: newDir toPath: 
      
        @"
      
      
        changeDir
      
      
        "
      
       error:NULL] ==
      
         NO) {


      
      
        23
      
                   NSLog(
      
        @"
      
      
        \nChange directory name failed
      
      
        "
      
      
        );


      
      
        24
      
      
        return
      
      
        2
      
      
        ;


      
      
        25
      
      
                }


      
      
        26
      
      
        27
      
      
        //
      
      
        更改當(dāng)前路徑
      
      
        28
      
      
        if
      
       ([fm changeCurrentDirectoryPath:
      
        @"
      
      
        changeDir
      
      
        "
      
      ] ==
      
         NO) {


      
      
        29
      
                   NSLog(
      
        @"
      
      
        \nChange current directory failed
      
      
        "
      
      
        );


      
      
        30
      
      
        return
      
      
        1
      
      
        ;


      
      
        31
      
      
                }


      
      
        32
      
               NSLog(
      
        @"
      
      
        \nAfter change current directory.....
      
      
        "
      
      
        );


      
      
        33
      
               currentPath =
      
         [fm currentDirectoryPath];


      
      
        34
      
               NSLog(
      
        @"
      
      
        \nCurrentpath is : \n%@
      
      
        "
      
      
        , currentPath);


      
      
        35
      
      
            }


      
      
        36
      
      
        return
      
      
        0
      
      
        ;


      
      
        37
      
       }
    

?

Foundation 框架 NSFileManager,NSData 簡(jiǎn)單的文件操作


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 亚洲av毛片一区二区久久 | 美女久久久久 | 免费无遮挡www小视频 | 天天爽夜夜爽 | 在线看片h站 | 国产欧美一区二区三区在线看 | 亚洲最大成人综合 | 欧美一区二区三区在线观看免费 | 狠狠色噜噜狠狠狠狠97影音先锋 | 国产1区2区3区| 欧美性猛交一区二区三区精品 | 国产萝控精品福利视频免费观看 | 久久精品中文 | 性 毛片 | 欧美人成网站 | 成人免费毛片在线观看 | 免费一级片 | 日韩a电影 | 亚洲黄页 | www.在线播放 | 日韩在线短视频 | 国产精品999在线观看 | 黄色视频a级毛片 | 欧美精品在线免费观看 | 亚洲精品黄色 | 欧美高清在线精品一区 | 欧美成a人片在线观看 | 日韩精品一区二区三区国语自制 | 久久伊人草 | 极品尤物一区二区三区 | 国产午夜精品一区二区三区嫩草 | 久久狠狠 | 九九在线精品视频 | 天天插一插 | 久操视频在线观看 | 亚洲成人av | 国产欧美在线视频 | 精品福利视频一区二区三区 | 一级大片免费看 | 极品美女aⅴ高清在线观看 一级片片 | 色婷婷综合缴情综六月 |