你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> 詳解iOS學習筆記(十七)——文件操作(NSFileManager)

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

編輯:IOS開發綜合

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:提供一個即時創建臨時文件的地方,但不需要持久化,在應用關閉後,該目錄下的數據將刪除,也可能系統在程序不運行的時候清除。

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

獲取應用沙盒根路徑:

-(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); 
} 

創建文件夾:

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

創建文件

 //創建文件 
-(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(@"文件創建成功: %@" ,testPath); 
  }else 
    NSLog(@"文件創建失敗"); 
} 

寫數據到文件:

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

讀文件數據:

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved