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

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Foundation 框架 NSFileManager,NSData 簡單的

系統 1739 0

一、簡單展示NSFileManager的使用

      
        #import
      
       <Foundation/Foundation.h>




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

{



    @autoreleasepool {

        
      
      
        //
      
      
        創建文件管理對象
      
      

        NSFileManager *fm =
      
         [NSFileManager defaultManager];

        
      
      
        //
      
      
        要操作的文件名
      
      

        NSString *fname = 
      
        @"
      
      
        myfile
      
      
        "
      
      
        ;

        
      
      
        //
      
      
        獲取文件的字典
      
      

        NSDictionary *
      
        attr;

        
      
      
        //
      
      
        當前路徑
      
      

        NSString *
      
        path;

        
      
      
        //
      
      
        獲取當前路徑
      
      

        path =
      
         [fm currentDirectoryPath];

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

        

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

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

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

            
      
      
        //
      
      
        return 0;
      
      
                }

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

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

            
      
      
        return
      
      
        1
      
      
        ;

        }

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

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

            
      
      
        return
      
      
        2
      
      
        ;

        }

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

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

            
      
      
        return
      
      
        3
      
      
        ;

        }

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

            NSLog(
      
      
        @"
      
      
        \nGet attributets failed
      
      
        "
      
      
        );

            
      
      
        return
      
      
        4
      
      
        ;

        }

        
      
      
        //
      
      
        文件大小
      
      

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

        
      
      
        //
      
      
        文件類型
      
      

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

        
      
      
        //
      
      
        創建者
      
      

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

        
      
      
        //


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

        
      
      
        //
      
      
        顯示文件內容
      
      

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

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

    }

    
      
      
        return
      
      
        0
      
      
        ;

}
      
    

?二、通過NSData完成副本制作

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


      
      
         2
      
      
        {


      
      
         3
      
      
         4
      
      
            @autoreleasepool {


      
      
         5
      
      
        //
      
      
        通過NSDate來完成文件副本制作
      
      
         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
      
      
        //
      
      
        將緩沖區NSData中的內容復制到文件中
      
      
        17
      
      
        if
      
       ([fm createFileAtPath:
      
        @"
      
      
        myFavoriteFile
      
      
        "
      
       contents: dt attributes:nil] ==
      
         NO) {


      
      
        18
      
                   NSLog(
      
        @"
      
      
        Creat backups failed
      
      
        "
      
      
        );


      
      
        19
      
      
        return
      
      
        1
      
      
        ;


      
      
        20
      
      
                }


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


      
      
        24
      
      
            }


      
      
        25
      
      
        return
      
      
        0
      
      
        ;


      
      
        26
      
       }
    

三、簡單的目錄操作

      
         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
      
      
        //
      
      
        獲取當前路徑
      
      
        12
      
               currentPath =
      
         [fm currentDirectoryPath];


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


      
      
        14
      
      
        15
      
      
        //
      
      
        在當前目錄下新建一個目錄
      
      
        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
      
      
        //
      
      
        更改當前路徑
      
      
        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 簡單的文件操作


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 天天插天天操 | 中文乱码字幕午夜无线观看 | 国产欧美一区二区精品仙草咪 | 国产专区日韩精品欧美色 | 午夜深夜福利网址 | 国产精品白丝喷水在线观看 | 99视频在线观看免费 | 另类欧美chinese | 欧美亚洲国产精品久久高清 | 久久国产亚洲精品麻豆 | 精品亚洲成a人在线播放 | 亚洲国产精品二区久久 | 国产欧美久久精品 | 亚洲日韩第一页 | 色婷婷综合久久久中文字幕 | 四虎影| 99自拍视频在线观看 | 最近中文字幕在线 | 中文 | 欧美性精品hd在线观看 | 99热久久这里只有精品首页 | 夜间福利视频 | 一级片短视频 | 国产毛片在线 | 久久久免费精品视频 | 九九婷婷| 日日夜夜骑 | 天天干天天干天天操 | 精品日韩一区二区三区 | 午夜不卡福利 | 国产欧美一区二区三区精品 | 成视频年人黄网站免费视频 | 国产福利观看 | 夜色视频网站 | 四虎永久网址在线观看 | 国产成人一区二区三区在线播放 | 甜心女孩泰剧在线观看 | 久久影院一区二区三区 | 欧美一二三区视频 | 久久久亚洲欧美综合 | 久久精品国产400部免费看 | 伊人久久婷婷丁香六月综合基地 |