国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

iOS學習筆記——文件操作(NSFileManager)

2019-11-14 19:11:46
字體:
來源:轉載
供稿:網友

 

iOS學習筆記(十七)——文件操作(NSFileManager)

       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 *content=@"測試寫入內容!";  
  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]?@"YES":@"NO");  
  13. }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 建宁县| 汽车| 清河县| 池州市| 大兴区| 湖口县| 多伦县| 永寿县| 宁安市| 靖州| 多伦县| 双鸭山市| 渭源县| 双辽市| 庆元县| 阿鲁科尔沁旗| 雅安市| 双峰县| 政和县| 阿克| 黄陵县| 中牟县| 乐山市| 巴彦淖尔市| 芜湖市| 定远县| 华亭县| 舒兰市| 诸城市| 兴宁市| 乌鲁木齐市| 平远县| 龙江县| 图们市| 章丘市| 称多县| 邳州市| 新巴尔虎右旗| 静宁县| 玉溪市| 乌兰浩特市|