IOS檔案操作

來源:互聯網
上載者:User

標籤:blog   class   tar   int   strong   http   

Objective-C檔案和目錄操作,IOS檔案操作,NSFileManager使用檔案操作:

 

objective-c通過使用NSFileManager類來管理和操作檔案、目錄,NSFileManager,檔案或目錄是使用檔案的路徑名的唯一標示。每個路徑名都是一個NSString對象。

NSFileManager對象通過defaultManager方法來建立執行個體
列如:

NSFileManager *fm = [NSFileManager defaultManager];

 

刪除某個檔案
[fm removeItemAtPath:@"filename" error:NULL];

error:參數是一個指向NSError對象的指標,能夠提供錯誤的資訊。如果指定為NULL的話就會使用預設的行為,傳回值是BOOL類型的方法,操作成功返回YES反之返回NO

 

判斷檔案是否被刪除
if([fm removeItemAtPath:@"filename" error:NULL]==NO){
NSLog(@"檔案刪除失敗");
return 1;
}

 

NSFileManager常用的檔案方法:

-(NSData*)contentsAtPath:path 從一個檔案中讀取資料

-(BOLL)createFileAtPath:path contents:(NSData*)data attributes: attr 向一個檔案寫入資料

-(BOOL)removeItemAtPath:path error:err 刪除一個檔案

-(BOOL)moveItemAtPath:from toPath:to error:err 重新命名或移動一個檔案(to 不能是已存在的)

-(BOOL)copyItemAtPath:from toPath:to error:err 複製檔案(to 不能是已存在的)

-(BOOL)contentsEqualAtPath:path1 andPath:path2 比較這兩個檔案的內容

-(BOOL)fileExistsAtPath:path 測試檔案是否存在

-(BOOL)isReadableFileAtPath:path 測試檔案是否存在,並且是否能執行讀操作

-(BOOL)isWritableFileAtPath:path 測試檔案是否存在,並且是否能執行寫操作

-(NSDictionary*)attributesOfItemAtPath:path error:err 擷取檔案的屬性

屬性字典允許你指定要建立的檔案的許可權,如果將該參數指定為nil,該檔案會被設定為預設許可權。

 

1、通過一段程式來對檔案進行操作:

 

[cpp] view plaincopyprint? 
  1. //  
  2. //  main.m  
  3. //  NSFileManager_01  
  4. //  
  5. //  Created by swinglife on 13-11-10.  
  6. //  Copyright (c) 2013年 swinglife. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10.   
  11. int main(int argc, const char * argv[])  
  12. {  
  13.       
  14.     @autoreleasepool {  
  15.         //檔案名稱  
  16.         NSString *fileName = @"testFile";  
  17.         NSString *fileContent = @"這是檔案內容!!!!";  
  18.         NSData *fileData = [fileContent dataUsingEncoding:NSUTF8StringEncoding];  
  19.           
  20.         //建立NSFileManager執行個體  
  21.         NSFileManager *fm = [NSFileManager defaultManager];  
  22.           
  23.         //建立檔案  
  24.         [fm createFileAtPath:fileName contents:fileData attributes:nil];  
  25.           
  26.         //判斷檔案是否存在 不存在就結束程式  
  27.         if([fm fileExistsAtPath:fileName]==NO){  
  28.             NSLog(@"檔案不存在");  
  29.             return 1;  
  30.         }  
  31.           
  32.         //拷貝檔案  
  33.         if([fm copyItemAtPath:fileName toPath:@"newFile" error:NULL]==NO){  
  34.             NSLog(@"複製失敗");  
  35.             return 2;  
  36.         }  
  37.           
  38.         //測試兩個檔案是否相同  
  39.         if([fm contentsEqualAtPath:fileName andPath:@"newFile"]==NO){  
  40.             NSLog(@"檔案不相同");  
  41.             return 3;  
  42.         }  
  43.           
  44.         //重新命名newFile  
  45.         [fm moveItemAtPath:@"newFile" toPath:@"newFile2" error:NULL];  
  46.           
  47.         //擷取newFile2的大小  
  48.         NSDictionary *fileAttr = [fm attributesOfItemAtPath:@"newFile2" error:NULL];  
  49.         if(fileAttr!=nil){  
  50.             NSLog(@"檔案大小:%llu bytes",[[fileAttr objectForKey:NSFileSize] unsignedLongLongValue]);  
  51.         }  
  52.           
  53.         //刪除檔案  
  54.         [fm removeItemAtPath:fileName error:NULL];  
  55.           
  56.         //顯示newFile2的內容  
  57.         NSString *data = [NSString stringWithContentsOfFile:@"newFile2" encoding:NSUTF8StringEncoding error:NULL];  
  58.         NSLog(@"%@",data);  
  59.           
  60.           
  61.     }  
  62.     return 0;  
  63. }  

 

 

NSFileManager常用的目錄方法


-(NSString*)currentDirectoryPath 擷取目前的目錄

-(BOOL)changeCurrentDirectoryPath:path 更改目前的目錄

-(BOOL)copyItemAtPath:from toPath:to error:err 複製目錄結構

-(BOOL)createDirectoryAtPath:path withIntermediateDirectories:(BOOL)flag attributes:attr 建立一個新目錄

-(BOOL)fileExistsAtPath:path isDirectory:(BOOL*)flag 測試檔案是不是目錄(flag中儲存結果)

-(NSArray*)contentsOfDirectoryAtPath:path error:err 列出目錄內容

-(NSDirectoryEnumerator*)enumeratorAtPath:path 枚舉目錄的內容

-(BOOL)removeItemAtPath:path error:err 刪除空目錄

-(BOOL)moveItemAtPath:from toPath:to error:err 重新命名或移動一個目錄

 

2、通過一段程式來對目錄進行操作:

 

[cpp] view plaincopyprint? 
    1. //  
    2. //  main.m  
    3. //  NSFileManager_02  
    4. //  
    5. //  Created by swinglife on 13-11-10.  
    6. //  Copyright (c) 2013年 swinglife. All rights reserved.  
    7. //  
    8.   
    9. #import <Foundation/Foundation.h>  
    10.   
    11. int main(int argc, const char * argv[])  
    12. {  
    13.   
    14.     @autoreleasepool {  
    15.         //檔案目錄  
    16.         NSString *dirName = @"testDir";  
    17.           
    18.         //建立NSFileManager執行個體  
    19.         NSFileManager *fm = [NSFileManager defaultManager];  
    20.           
    21.         //擷取目前的目錄  
    22.         NSString *path = [fm currentDirectoryPath];  
    23.         NSLog(@"Path:%@",path);  
    24.           
    25.         //建立新目錄  
    26.         [fm createDirectoryAtPath:dirName withIntermediateDirectories:YES attributes:nil error:NULL];  
    27.           
    28.         //重新命名新的目錄  
    29.         [fm moveItemAtPath:dirName toPath:@"newDir" error:NULL];  
    30.           
    31.         //更改目前的目錄到新的目錄  
    32.         [fm changeCurrentDirectoryPath:@"newDir"];  
    33.           
    34.         //擷取當前工作目錄  
    35.         path = [fm currentDirectoryPath];  
    36.         NSLog(@"Path:%@",path);  
    37.           
    38.     }  
    39.     return 0;  
    40. }  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.