最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2016年12月12日 16:22:35   作者:張興業(yè)  
這篇文章主要介紹了詳解iOS學習筆記(十七)——文件操作(NSFileManager),具有一定的參考價值,有需要的可以了解一下。

iOS的沙盒機制,應(yīng)用只能訪問自己應(yīng)用目錄下的文件。iOS不像Android,沒有SD卡概念,不能直接訪問圖像、視頻等內(nèi)容。iOS應(yīng)用產(chǎn)生的內(nèi)容,如圖像、文件、緩存內(nèi)容等都必須存儲在自己的沙盒內(nèi)。默認情況下,每個沙盒含有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:存儲程序的默認設(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)在程序不運行的時候清除。

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

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

-(void)dirHome{ 
  NSString *dirHome=NSHomeDirectory();   
  NSLog(@"app_home: %@",dirHome); 
} 

獲取Documents目錄路徑:

//獲取Documents目錄 
-(NSString *)dirDoc{ 
  //[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
  NSString *documentsDirectory = [paths objectAtIndex:0]; 
  NSLog(@"app_home_doc: %@",documentsDirectory); 
  return documentsDirectory; 
} 

獲取Library目錄路徑:

//獲取Library目錄 
-(void)dirLib{ 
  //[NSHomeDirectory() stringByAppendingPathComponent:@"Library"]; 
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); 
  NSString *libraryDirectory = [paths objectAtIndex:0]; 
  NSLog(@"app_home_lib: %@",libraryDirectory); 
} 

獲取Cache目錄路徑:

//獲取Cache目錄 
-(void)dirCache{ 
  NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
  NSString *cachePath = [cacPath objectAtIndex:0]; 
  NSLog(@"app_home_lib_cache: %@",cachePath); 
} 

獲取Tmp目錄路徑:

//獲取Tmp目錄 
-(void)dirTmp{ 
  //[NSHomeDirectory() stringByAppendingPathComponent:@"tmp"]; 
  NSString *tmpDirectory = NSTemporaryDirectory(); 
  NSLog(@"app_home_tmp: %@",tmpDirectory); 
} 

創(chuàng)建文件夾:

//創(chuàng)建文件夾 
-(void *)createDir{ 
  NSString *documentsPath =[self dirDoc]; 
  NSFileManager *fileManager = [NSFileManager defaultManager]; 
  NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"]; 
  // 創(chuàng)建目錄 
  BOOL res=[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil]; 
  if (res) { 
    NSLog(@"文件夾創(chuàng)建成功"); 
  }else 
    NSLog(@"文件夾創(chuàng)建失敗"); 
 } 

創(chuàng)建文件

 //創(chuàng)建文件 
-(void *)createFile{ 
  NSString *documentsPath =[self dirDoc]; 
  NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"]; 
  NSFileManager *fileManager = [NSFileManager defaultManager]; 
  NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"]; 
  BOOL res=[fileManager createFileAtPath:testPath contents:nil attributes:nil]; 
  if (res) { 
    NSLog(@"文件創(chuàng)建成功: %@" ,testPath); 
  }else 
    NSLog(@"文件創(chuàng)建失敗"); 
} 

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

//寫文件 
-(void)writeFile{ 
  NSString *documentsPath =[self dirDoc]; 
  NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"]; 
  NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"]; 
  NSString *content=@"測試寫入內(nèi)容!"; 
  BOOL res=[content writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil]; 
  if (res) { 
    NSLog(@"文件寫入成功"); 
  }else 
    NSLog(@"文件寫入失敗"); 
} 

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

//讀文件 
-(void)readFile{ 
  NSString *documentsPath =[self dirDoc]; 
  NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"]; 
  NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"]; 
//  NSData *data = [NSData dataWithContentsOfFile:testPath]; 
//  NSLog(@"文件讀取成功: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); 
  NSString *content=[NSString stringWithContentsOfFile:testPath encoding:NSUTF8StringEncoding error:nil]; 
  NSLog(@"文件讀取成功: %@",content); 
} 

文件屬性:

//文件屬性 
-(void)fileAttriutes{ 
  NSString *documentsPath =[self dirDoc]; 
  NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"]; 
  NSFileManager *fileManager = [NSFileManager defaultManager]; 
  NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"]; 
  NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:testPath error:nil];   
  NSArray *keys; 
  id key, value; 
  keys = [fileAttributes allKeys]; 
  int count = [keys count]; 
  for (int i = 0; i < count; i++) 
  { 
    key = [keys objectAtIndex: i]; 
    value = [fileAttributes objectForKey: key]; 
    NSLog (@"Key: %@ for value: %@", key, value); 
  } 
} 

刪除文件:

//刪除文件 
-(void)deleteFile{ 
  NSString *documentsPath =[self dirDoc]; 
  NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"]; 
  NSFileManager *fileManager = [NSFileManager defaultManager]; 
  NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];   
  BOOL res=[fileManager removeItemAtPath:testPath error:nil]; 
  if (res) { 
    NSLog(@"文件刪除成功"); 
  }else 
    NSLog(@"文件刪除失敗");   
  NSLog(@"文件是否存在: %@",[fileManager isExecutableFileAtPath:testPath]?@"YES":@"NO"); 
} 

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 分享一個關(guān)于Storyboard 跳轉(zhuǎn)與傳值

    分享一個關(guān)于Storyboard 跳轉(zhuǎn)與傳值

    近日不忙,給大家分享一個關(guān)于storyboard跳轉(zhuǎn)傳值的相關(guān)知識,感興趣的朋友一起看看吧
    2015-12-12
  • Swift 去除 TableView 多余的空Cell中的橫線的方法

    Swift 去除 TableView 多余的空Cell中的橫線的方法

    這篇文章主要介紹了Swift 去除 TableView 多余的空Cell中的橫線的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-02-02
  • 在iOS中實現(xiàn)谷歌滅霸彩蛋的完整示例

    在iOS中實現(xiàn)谷歌滅霸彩蛋的完整示例

    這篇文章主要給大家介紹了關(guān)于如何在iOS中實現(xiàn)谷歌滅霸彩蛋的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對各位iOS開發(fā)者們具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-05-05
  • iOS實現(xiàn)文字水平無間斷滾動效果

    iOS實現(xiàn)文字水平無間斷滾動效果

    這篇文章主要為大家詳細介紹了iOS實現(xiàn)文字水平無間斷滾動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • iOS改變UITextField占位文字顏色的三種方法

    iOS改變UITextField占位文字顏色的三種方法

    這篇文章主要為大家詳細介紹了iOS改變UITextField的占位文字顏色的三種方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • Ios蘋果app應(yīng)用程序開發(fā)者如何獲取IPA簽名證書詳解

    Ios蘋果app應(yīng)用程序開發(fā)者如何獲取IPA簽名證書詳解

    這篇文章主要為大家介紹了Ios蘋果app應(yīng)用程序開發(fā)者如何獲取IPA簽名證書詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-11-11
  • 簡單說說iOS之WKWebView的用法小結(jié)

    簡單說說iOS之WKWebView的用法小結(jié)

    iOS8.0之后我們使用 WebKit框架中的WKWebView來加載網(wǎng)頁。這篇文章主要介紹了簡單說說iOS之WKWebView的用法小結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • iOS消息推送原理及具體實現(xiàn)代碼

    iOS消息推送原理及具體實現(xiàn)代碼

    這篇文章主要為大家詳細介紹了iOS 消息推送原理及具體實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • iOS 在線視頻生成GIF圖功能的方法

    iOS 在線視頻生成GIF圖功能的方法

    本篇文章主要介紹了iOS 在線視頻生成GIF圖功能的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • iOS Moya實現(xiàn)OAuth請求的方法

    iOS Moya實現(xiàn)OAuth請求的方法

    這篇文章主要介紹了iOS Moya實現(xiàn)OAuth請求的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12

最新評論

吉安县| 沅陵县| 西丰县| 兰西县| 桐梓县| 元谋县| 垣曲县| 余庆县| 汉阴县| 临猗县| 丹阳市| 青岛市| 侯马市| 横山县| 南皮县| 陇西县| 彰武县| 三明市| 屯留县| 濮阳市| 孟村| 游戏| 壤塘县| 蒲城县| 兴仁县| 盐城市| 北川| 永福县| 台北市| 敦煌市| 罗山县| 长岭县| 铜山县| 阜宁县| 长白| 开封县| 云安县| 遂平县| 乌兰浩特市| 德化县| 清涧县|