亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

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

Documents:蘋(píng)果建議將程序創(chuàng)建產(chǎn)生的文件以及應(yīng)用瀏覽產(chǎn)生的文件數(shù)據(jù)保存在該目錄下,iTunes備份和恢復(fù)的時(shí)候會(huì)包括此目錄

Library:存儲(chǔ)程序的默認(rèn)設(shè)置或其它狀態(tài)信息;

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

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

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)建失敗"); 
} 

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

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

讀文件數(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"); 
} 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS如何讓tableview支持不同種類(lèi)的cell詳解

    iOS如何讓tableview支持不同種類(lèi)的cell詳解

    這篇文章主要給大家介紹了關(guān)于iOS如何讓tableview支持不同種類(lèi)的cell的相關(guān)資料,文中通過(guò)示例代碼詳細(xì)的給大家介紹了實(shí)現(xiàn)的兩種方法,對(duì)各位iOS開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-08-08
  • 全面解析iOS應(yīng)用中自定義UITableViewCell的方法

    全面解析iOS應(yīng)用中自定義UITableViewCell的方法

    這篇文章主要介紹了iOS應(yīng)用開(kāi)發(fā)中自定義UITableViewCell的方法,示例為傳統(tǒng)的Obejective-C語(yǔ)言,需要的朋友可以參考下
    2016-04-04
  • fastlane自動(dòng)化打包iOS APP過(guò)程示例

    fastlane自動(dòng)化打包iOS APP過(guò)程示例

    這篇文章主要為大家介紹了fastlane自動(dòng)化打包iOS APP的過(guò)程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • iOS計(jì)算上次日期距離現(xiàn)在多久的代碼

    iOS計(jì)算上次日期距離現(xiàn)在多久的代碼

    這篇文章主要為大家詳細(xì)介紹了iOS計(jì)算上次日期距離現(xiàn)在多久的代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • IOS中無(wú)限滾動(dòng)Scrollview效果

    IOS中無(wú)限滾動(dòng)Scrollview效果

    這篇文章主要為大家詳細(xì)介紹了IOS中無(wú)限滾動(dòng)Scrollview效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-02-02
  • iOS應(yīng)用開(kāi)發(fā)中UIScrollView滾動(dòng)視圖的基本用法總結(jié)

    iOS應(yīng)用開(kāi)發(fā)中UIScrollView滾動(dòng)視圖的基本用法總結(jié)

    這篇文章主要介紹了iOS應(yīng)用開(kāi)發(fā)中UIScrollView滾動(dòng)視圖的基本用法總結(jié),作者還介紹了重寫(xiě)UIScrollView中的hitTest方法來(lái)解決長(zhǎng)按的事件問(wèn)題,需要的朋友可以參考下
    2016-02-02
  • iOS中程序異常Crash友好化處理詳解

    iOS中程序異常Crash友好化處理詳解

    在iOS開(kāi)發(fā)調(diào)試過(guò)程中以及上線之后,程序經(jīng)常會(huì)出現(xiàn)崩潰的問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于iOS中程序異常Crash友好化處理的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2018-07-07
  • 學(xué)習(xí)iOS開(kāi)關(guān)按鈕UISwitch控件

    學(xué)習(xí)iOS開(kāi)關(guān)按鈕UISwitch控件

    這篇文章主要為大家詳細(xì)介紹了iOS開(kāi)關(guān)按鈕UISwitch控件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • iOS 真機(jī)調(diào)試微信小程序

    iOS 真機(jī)調(diào)試微信小程序

    下面介紹下如何使用 Xcode、Reveal 和 Safari 來(lái)真機(jī)調(diào)試 iOS 上的小程序。感興趣的朋友跟隨腳本之家小編一起看看吧
    2018-08-08
  • iOS二維碼的生成代碼

    iOS二維碼的生成代碼

    這篇文章主要為大家詳細(xì)介紹了iOS二維碼的生成代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08

最新評(píng)論