日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

iOS學(xué)習(xí)筆記(十七)

 Rdxer館 2016-07-22

       iOS的沙盒機(jī)制,應(yīng)用只能訪問自己應(yīng)用目錄下的文件。iOS不像android,沒有SD卡概念,不能直接訪問圖像、視頻等內(nèi)容。iOS應(yīng)用產(chǎn)生的內(nèi)容,如圖像、文件、緩存內(nèi)容等都必須存儲在自己的沙盒內(nèi)。默認(rèn)情況下,每個沙盒含有3個文件夾:Documents, Library 和 tmp。Library包含Caches、Preferences目錄。

             

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

Documents:蘋果建議將程序創(chuàng)建產(chǎn)生的文件以及應(yīng)用瀏覽產(chǎn)生的文件數(shù)據(jù)保存在該目錄下,iTunes備份和恢復(fù)的時候會包括此目錄
Library:存儲程序的默認(rèn)設(shè)置或其它狀態(tài)信息;

Library/Caches:存放緩存文件,保存應(yīng)用的持久化數(shù)據(jù),用于應(yīng)用升級或者應(yīng)用關(guān)閉后的數(shù)據(jù)保存,不會被itunes同步,所以為了減少同步的時間,可以考慮將一些比較大的文件而又不需要備份的文件放到這個目錄下。

tmp:提供一個即時創(chuàng)建臨時文件的地方,但不需要持久化,在應(yīng)用關(guān)閉后,該目錄下的數(shù)據(jù)將刪除,也可能系統(tǒng)在程序不運行的時候清除。

               


APP  Sandbox

iOS怎么獲取沙盒路徑,怎么操作文件呢?下面給出答案。


獲取應(yīng)用沙盒根路徑:

  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. }  

創(chuàng)建文件夾:

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


創(chuàng)建文件

  1. //創(chuàng)建文件  
  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(@"文件創(chuàng)建成功: %@" ,testPath);  
  10.     }else  
  11.         NSLog(@"文件創(chuàng)建失敗");  
  12. }  


寫數(shù)據(jù)到文件:

  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=@"測試寫入內(nèi)容!";  
  7.     BOOL res=[content writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil];  
  8.     if (res) {  
  9.         NSLog(@"文件寫入成功");  
  10.     }else  
  11.         NSLog(@"文件寫入失敗");  
  12. }  

讀文件數(shù)據(jù):

  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. }  



/**
* @author 張興業(yè)
*  iOS入門群:83702688
*  android開發(fā)進(jìn)階群:241395671
*  我的新浪微博:@張興業(yè)TBOW
*  我的郵箱:xy-zhang#163.com#->@)
*/

https://developer.apple.com/library/ios/#documentation/FileManagement/Conceptual/FileSystemProgrammingGUide/FileSystemOverview/FileSystemOverview.html#//apple_ref/doc/uid/TP40010672-CH2-SW2

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多