iOS學習筆記——檔案操作(NSFileManager)

來源:互聯網
上載者:User

標籤:

  iOS的沙箱機制,應用只能訪問自己應用目錄下的檔案。iOS不像android,沒有SD卡概念,不能直接存取映像、視頻等內容。iOS應用產生的內容,像、檔案、緩衝內容等都必須儲存在自己的沙箱內。預設情況下,每個沙箱含有3個檔案夾:Documents, Library 和 tmp。Library包含Caches、Preferences目錄。

             

上面的完整路徑為:使用者->資產庫->Application Support->iPhone Simulator->6.1->Aplications

 

Documents:蘋果建議將程式建立產生的檔案以及應用瀏覽產生的檔案資料儲存在該目錄下,iTunes備份和恢複的時候會包括此目錄
Library:儲存程式的預設設定或其它狀態資訊;

Library/Caches:存放快取檔案,儲存應用的持久化資料,用於應用升級或者應用關閉後的資料儲存,不會被itunes同步,所以為了減少同步的時間,可以考慮將一些比較大的檔案而又不需要備份的檔案放到這個目錄下。

tmp:提供一個即時建立臨時檔案的地方,但不需要持久化,在應用關閉後,該目錄下的資料將刪除,也可能系統在程式不啟動並執行時候清除。

               

APP  Sandbox

iOS怎麼擷取沙箱路徑,怎麼操作檔案呢?下面給出答案。

 

擷取應用沙箱根路徑:
  1. -(void)dirHome{  
  2.     NSString *dirHome=NSHomeDirectory();      
  3.     NSLog(@"app_home: %@",dirHome);  
  4. }  

擷取Documents目錄路徑:

 

  1. //擷取Documents目錄  
  2. -(NSString *)dirDoc{  
  3.     //[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];  
  4.     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  5.     NSString *documentsDirectory = [paths objectAtIndex:0];  
  6.     NSLog(@"app_home_doc: %@",documentsDirectory);  
  7.     return documentsDirectory;  
  8. }  

擷取Library目錄路徑:

 

  1. //擷取Library目錄  
  2. -(void)dirLib{  
  3.     //[NSHomeDirectory() stringByAppendingPathComponent:@"Library"];  
  4.     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);  
  5.     NSString *libraryDirectory = [paths objectAtIndex:0];  
  6.     NSLog(@"app_home_lib: %@",libraryDirectory);  
  7. }  

擷取Cache目錄路徑:
  1. //擷取Cache目錄  
  2. -(void)dirCache{  
  3.     NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);  
  4.     NSString *cachePath = [cacPath objectAtIndex:0];  
  5.     NSLog(@"app_home_lib_cache: %@",cachePath);  
  6. }  

擷取Tmp目錄路徑:
  1. //擷取Tmp目錄  
  2. -(void)dirTmp{  
  3.     //[NSHomeDirectory() stringByAppendingPathComponent:@"tmp"];  
  4.     NSString *tmpDirectory = NSTemporaryDirectory();  
  5.     NSLog(@"app_home_tmp: %@",tmpDirectory);  
  6. }  

建立檔案夾:
  1. //建立檔案夾  
  2. -(void *)createDir{  
  3.     NSString *documentsPath =[self dirDoc];  
  4.     NSFileManager *fileManager = [NSFileManager defaultManager];  
  5.     NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];  
  6.     // 建立目錄  
  7.     BOOL res=[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];  
  8.     if (res) {  
  9.         NSLog(@"檔案夾建立成功");  
  10.     }else  
  11.         NSLog(@"檔案夾建立失敗");  
  12.  }  


建立檔案
  1. //建立檔案  
  2. -(void *)createFile{  
  3.     NSString *documentsPath =[self dirDoc];  
  4.     NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];  
  5.     NSFileManager *fileManager = [NSFileManager defaultManager];  
  6.     NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];  
  7.     BOOL res=[fileManager createFileAtPath:testPath contents:nil attributes:nil];  
  8.     if (res) {  
  9.         NSLog(@"檔案建立成功: %@" ,testPath);  
  10.     }else  
  11.         NSLog(@"檔案建立失敗");  
  12. }  

寫資料到檔案:
  1. //寫檔案  
  2. -(void)writeFile{  
  3.     NSString *documentsPath =[self dirDoc];  
  4.     NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];  
  5.     NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];  
  6.     NSString *[email protected]"測試寫入內容!";  
  7.     BOOL res=[content writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil];  
  8.     if (res) {  
  9.         NSLog(@"檔案寫入成功");  
  10.     }else  
  11.         NSLog(@"檔案寫入失敗");  
  12. }  

讀檔案資料:
  1. //讀檔案  
  2. -(void)readFile{  
  3.     NSString *documentsPath =[self dirDoc];  
  4.     NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];  
  5.     NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];  
  6. //    NSData *data = [NSData dataWithContentsOfFile:testPath];  
  7. //    NSLog(@"檔案讀取成功: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);  
  8.     NSString *content=[NSString stringWithContentsOfFile:testPath encoding:NSUTF8StringEncoding error:nil];  
  9.     NSLog(@"檔案讀取成功: %@",content);  
  10. }  

檔案屬性:
  1. //檔案屬性  
  2. -(void)fileAttriutes{  
  3.     NSString *documentsPath =[self dirDoc];  
  4.     NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];  
  5.     NSFileManager *fileManager = [NSFileManager defaultManager];  
  6.     NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];  
  7.     NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:testPath error:nil];     
  8.     NSArray *keys;  
  9.     id key, value;  
  10.     keys = [fileAttributes allKeys];  
  11.     int count = [keys count];  
  12.     for (int i = 0; i < count; i++)  
  13.     {  
  14.         key = [keys objectAtIndex: i];  
  15.         value = [fileAttributes objectForKey: key];  
  16.         NSLog (@"Key: %@ for value: %@", key, value);  
  17.     }  
  18. }  

刪除檔案:

  

  1. //刪除檔案  
  2. -(void)deleteFile{  
  3.     NSString *documentsPath =[self dirDoc];  
  4.     NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];  
  5.     NSFileManager *fileManager = [NSFileManager defaultManager];  
  6.     NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];     
  7.     BOOL res=[fileManager removeItemAtPath:testPath error:nil];  
  8.     if (res) {  
  9.         NSLog(@"檔案刪除成功");  
  10.     }else  
  11.         NSLog(@"檔案刪除失敗");     
  12.     NSLog(@"檔案是否存在: %@",[fileManager isExecutableFileAtPath:testPath][email protected]"YES":@"NO");  
  13. }
  

iOS學習筆記——檔案操作(NSFileManager)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.