iphone沙箱模型的有四個文件夾,分別是什麼,永久數據存儲一般放在什麼位置,得到模擬器的路徑的簡單方式是什麼.
documents,tmp,app,Library。
(NSHomeDirectory()),
手動保存的文件在documents文件裏
Nsuserdefaults保存的文件在tmp文件夾裏
1、Documents 目錄:您應該將所有de應用程序數據文件寫入到這個目錄下。這個目錄用於存儲用戶數據或其它應該定期備份的信息。
2、AppName.app 目錄:這是應用程序的程序包目錄,包含應用程序的本身。由於應用程序必須經過簽名,所以您在運行時不能對這個目錄中的內容進行修改,否則可能會使應用程序無法啓動。
3、Library
目錄:這個目錄下有兩個子目錄:Caches 和 Preferences
Preferences 目錄:包含應用程序的偏好設置文件。您不應該直接創建偏好設置文件,而是應該使用NSUserDefaults類來取得和設置應用程序的偏好.
Caches 目錄:用於存放應用程序專用的支持文件,保存應用程序再次啓動過程中需要的信息。
4、tmp 目錄:這個目錄用於存放臨時文件,保存應用程序再次啓動過程中不需要的信息。
獲取這些目錄路徑的方法:
1,獲取家目錄路徑的函數:
NSString *homeDir = NSHomeDirectory();
2,獲取Documents目錄路徑的方法:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
3,獲取Caches目錄路徑的方法:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDir = [paths objectAtIndex:0];
4,獲取tmp目錄路徑的方法:
NSString *tmpDir = NSTemporaryDirectory();
5,獲取應用程序程序包中資源文件路徑的方法:
例如獲取程序包中一個圖片資源(apple.png)路徑的方法:
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@”apple” ofType:@”png”];
UIImage *appleImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
代碼中的mainBundle類方法用於返回一個代表應用程序包的對象。
- // 獲取沙盒主目錄路徑
- NSString *homeDir = NSHomeDirectory();
- // 獲取Documents目錄路徑
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *docDir = [paths objectAtIndex:0];
- // 獲取Caches目錄路徑
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
- NSString *cachesDir = [paths objectAtIndex:0];
- // 獲取tmp目錄路徑
- NSString *tmpDir = NSTemporaryDirectory();
- // 獲取當前程序包中一個圖片資源(apple.png)路徑
- NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"apple" ofType:@"png"];
- UIImage *appleImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
例子:
NSFileManager*
fm=[NSFileManager defaultManager];
if(![fm fileExistsAtPath:[self dataFilePath]]){
//下面是對該文件進行制定路徑的保存
[fm createDirectoryAtPath:[self dataFilePath] withIntermediateDirectories:YES attributes:nil error:nil];
//取得一個目錄下得所有文件名
NSArray *files = [fm subpathsAtPath: [self dataFilePath] ];
//讀取某個文件
NSData *data = [fm contentsAtPath:[self dataFilePath]];
//或者
NSData *data = [NSData dataWithContentOfPath:[self dataFilePath]];
}
1、常見的NSFileManager文件方法
-(NSData *)contentsAtPath:path //從一個文件讀取數據
-(BOOL)createFileAtPath: path contents:(NSData *)data attributes:attr //向一個文件寫入數據
-(BOOL)removeItemAtPath:path error:err //刪除一個文件
-(BOOL)moveItemAtPath:from toPath:to error:err //重命名或者移動一個文件(to不能是已存在的)
-(BOOL)copyItemAtPath:from toPath:to error:err //複製文件(to不能是已存在的)
-(BOOL)contentsEqualAtPath:path andPath:path2 //比較兩個文件的內容
-(BOOL)fileExistAtPath:path //測試文件是否存在
-(BOOL)isReadableFileAtPath:path //測試文件是否存在,並且是否能執行讀操作
-(BOOL)isWriteableFileAtPath:path //測試文件是否存在,並且是否能執行寫操作
-(NSDictionary *)attributesOfItemAtPath:path error:err //獲取文件的屬性
-(BOOL)setAttributesOfItemAtPath:attr error:err //更改文件的屬性
2.使用目錄
-(NSString *)currentDirectoryPath //獲取當前目錄
-(BOOL)changeCurrentDirectoryPath:path //更改當前目錄
-(BOOL)copyItemAtPath:from toPath:to error:err //複製目錄結構(to不能是已存在的)
-(BOOL)createDirectoryAtPath:path withIntermediateDirectories:(BOOL)flag attribute:attr //創建一個新目錄
-(BOOL)fileExistAtPath:path isDirectory:(BOOL*)flag //測試文件是不是目錄(flag中儲存結果YES/NO)
-(NSArray *)contentsOfDirectoryAtPath:path error:err //列出目錄內容
-(NSDirectoryEnumerator *)enumeratorAtPath:path //枚舉目錄的內容
-(BOOL)removeItemAtPath:path error:err //刪除空目錄
-(BOOL)moveItemAtPath:from toPath:to error:err //重命名或移動一個目錄(to不能是已存在的)
3、常用路徑工具方法
+(NSString *)pathWithComponens:components //根據components中的元素構造有效路徑
-(NSArray *)pathComponents //析構路徑,獲得組成此路徑的各個部分
-(NSString *)lastPathComponent //提取路徑的最後一個組成部分
-(NSString *)pathExtension //從路徑的最後一個組成部分中提取其擴展名
-(NSString *)stringByAppendingPathComponent:path //將path添加到現有路徑的末尾
-(NSString *)stringByAppendingPathExtension:ext //將指定的擴展名添加到路徑的最後一個組成部分
-(NSString *)stringByDeletingLastPathComponent //刪除路徑的最後一個組成部分
-(NSString *)stringByDeletingPathExtension //從文件的最後一部分刪除擴展名
-(NSString *)stringByExpandingTileInPath //將路徑中代字符擴展成用戶主目錄(~)或指定用戶的主目錄(~user)
-(NSString *)stringByresolvingSymlinksInPath //嘗試解析路徑中的符號鏈接
-(NSString *)stringByStandardizingPath //通過嘗試解析~、..(父目錄符號)、.(當前目錄符號)和符號鏈接來標準化路徑
4、常用的路徑工具函數
NSString* NSUserName(void) //返回當前用戶的登錄名
NSString* NSFullUserName(void) //返回當前用戶的完整用戶名
NSString* NSHomeDirectory(void) //返回當前用戶主目錄的路徑
NSString* NSHomeDirectoryForUser(NSString* user) //返回用戶user的主目錄
NSString* NSTemporaryDirectory(void) //返回可用於創建臨時文件的路徑目錄
5、常用的IOS目錄
Documents(NSDocumentDirectory) //用於寫入應用相關數據文件的目錄,在ios中寫入這裏的文件能夠與iTunes共享並訪問,存儲在這裏的文件會自動備份到雲端
Library/Caches(NSCachesDirectory) //用於寫入應用支持文件的目錄,保存應用程序再次啓動需要的信息。iTunes不會對這個目錄的內容進行備份
tmp(use NSTemporaryDirectory()) //這個目錄用於存放臨時文件,只程序終止時需要移除這些文件,當應用程序不再需要這些臨時文件時,應該將其從這個目錄中刪除
Library/Preferences //這個目錄包含應用程序的偏好設置文件,使用 NSUserDefault類進行偏好設置文件的創建、讀取和修改