iOS中NSSearchPathForDirectoriesInDomains函數

轉載博客主:liliangchw

iOS中NSSearchPathForDirectoriesInDomains函數參數 NSDocumentDirectory, NSDocumentationDirectory, NSDownloadsDirectory的意義

剛在寫程序的時候把全部參數都測試了下,這樣本身能夠記住,下面是測試結果:
NSDocumentDirectory 緩存

-(NSString *) dataFilePath { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory=[paths objectAtIndex:0]; return [documentsDirectory stringByAppendingPathComponent:@"data.plisg"]; }


return value: app

path:/Users/admin/Library/Application Support/iPhone Simulator/5.0/Applications/4BC5DA66-B3CA-4056-927B-999BC4DBF3CE/Documents/data.plist

NSDocumentationDirectory ssh

-(NSString *) dataFilePath { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory , NSUserDomainMask, YES); NSString *documentsDirectory=[paths objectAtIndex:0]; return [documentsDirectory stringByAppendingPathComponent:@"data.plisg" ]; }

return value: 函數

path:/Users/admin/Library/Application Support/iPhone Simulator/5.0/Applications/4BC5DA66-B3CA-4056-927B-999BC4DBF3CE/Library/Documentation/data.plist

NSDownloadsDirectory 測試

-(NSString *) dataFilePath { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDownloadsDirectory, NSUserDomainMask, YES); NSString *documentsDirectory=[paths objectAtIndex:0]; return [documentsDirectory stringByAppendingPathComponent:@"data.plisg" ]; }

return value: 編碼

path:/Users/admin/Library/Application Support/iPhone Simulator/5.0/Applications/4BC5DA66-B3CA-4056-927B-999BC4DBF3CE/Downloads/data.plisg


轉自:http://marshal.easymorse.com/archives/3340

iOS中對文件的操做

由於應用是在沙箱(sandbox)中的,在文件讀寫權限上受到限制,只能在幾個目錄下讀寫文件: atom

  • Documents:應用中用戶數據能夠放在這裏,iTunes備份和恢復的時候會包括此目錄
  • tmp:存放臨時文件,iTunes不會備份和恢復此目錄,此目錄下文件可能會在應用退出後刪除
  • Library/Caches:存放緩存文件,iTunes不會備份此目錄,此目錄下文件不會在應用退出刪除

在Documents目錄下建立文件

代碼以下: spa

NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory
                                            , NSUserDomainMask 
                                            , YES); 
NSLog(@"Get document path: %@",[paths objectAtIndex:0]); .net

NSString *fileName=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"myFile"]; 
NSString *content=@"a"; 
NSData *contentData=[content dataUsingEncoding:NSASCIIStringEncoding]; 
if ([contentData writeToFile:fileName atomically:YES]) {
    NSLog(@">>write ok."); 
} code

能夠經過ssh登陸設備看到Documents目錄下生成了該文件。

上述是建立ascii編碼文本文件,若是要帶漢字,好比:

NSString *fileName=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"myFile"]; 
NSString *content=@"更深夜靜人已息"; 
NSData *contentData=[content dataUsingEncoding:NSUnicodeStringEncoding]; 
if ([contentData writeToFile:fileName atomically:YES]) {
    NSLog(@">>write ok."); 
}

若是還用ascii編碼,將不會生成文件。這裏使用NSUnicodeStringEncoding就能夠了。

經過filezilla下載到建立的文件打開,中文沒有問題:

image

在其餘目錄下建立文件

若是要指定其餘文件目錄,好比Caches目錄,須要更換目錄工廠常量,上面代碼其餘的可不變:

NSArray *paths=NSSearchPathForDirectoriesInDomains(NSCachesDirectory
                                                , NSUserDomainMask 
                                                , YES);

 

使用NSSearchPathForDirectoriesInDomains只能定位Caches目錄和Documents目錄。

tmp目錄,不能按照上面的作法得到目錄了,有個函數能夠得到應用的根目錄:

NSHomeDirectory()

也就是Documents的上級目錄,固然也是tmp目錄的上級目錄。那麼文件路徑能夠這樣寫:

NSString *fileName=[NSHomeDirectory() stringByAppendingPathComponent:@"tmp/myFile.txt"];

或者,更直接一點,能夠用這個函數:

NSTemporaryDirectory()

不過生成的路徑將多是:

…/tmp/-Tmp-/myFile.txt

使用資源文件

在編寫應用項目的時候,經常會使用資源文件,好比:

image

安裝到設備上後,是在app目錄下的:

image

如下代碼演示如何獲取到文件並打印文件內容:

NSString *myFilePath = [[NSBundle mainBundle] 
                        pathForResource:@"f" 
                        ofType:@"txt"]; 
NSString *myFileContent=[NSString stringWithContentsOfFile:myFilePath encoding:NSUTF8StringEncoding error:nil]; 
NSLog(@"bundel file path: %@ \nfile content:%@",myFilePath,myFileContent);

 

代碼運行效果:

image

相關文章
相關標籤/搜索