ios 中對文件的操作

轉自:Orand's Blog — 平凡的思考

iOS中NSSearchPathForDirectoriesInDomains函數


iOS中NSSearchPathForDirectoriesInDomains函數參數 NSDocumentDirectory, NSDocumentationDirectory, NSDownloadsDirectory的意義
剛學iOS對他的一些基本函數不太瞭解,在寫程序的時候把所有參數都測試了下,這樣自己可以記住,下面是測試結果:
NSDocumentDirectory

-(NSString *) dataFilePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 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/Documents/data.plist

NSDocumentationDirectory

-(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)中的,在文件讀寫權限上受到限制,只能在幾個目錄下讀寫文件:

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

在Documents目錄下創建文件

代碼如下:

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

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

可以通過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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章