iphone沙箱模型的有四個(gè)文件夾,分別是什么,永久數(shù)據(jù)存儲(chǔ)一般放在什么位置,得到模擬器的路徑的簡(jiǎn)單方式是什么.
documents,tmp,app,Library。
(NSHomeDirectory()),
手動(dòng)保存的文件在documents文件里
Nsuserdefaults保存的文件在tmp文件夾里
1、Documents 目錄:您應(yīng)該將所有de應(yīng)用程序數(shù)據(jù)文件寫入到這個(gè)目錄下。這個(gè)目錄用于存儲(chǔ)用戶數(shù)據(jù)或其它應(yīng)該定期備份的信息。
2、AppName.app 目錄:這是應(yīng)用程序的程序包目錄,包含應(yīng)用程序的本身。由于應(yīng)用程序必須經(jīng)過簽名,所以您在運(yùn)行時(shí)不能對(duì)這個(gè)目錄中的內(nèi)容進(jìn)行修改,否則可能會(huì)使應(yīng)用程序無法啟動(dòng)。
3、Library 目錄:這個(gè)目錄下有兩個(gè)子目錄:Caches 和 Preferences
Preferences 目錄:包含應(yīng)用程序的偏好設(shè)置文件。您不應(yīng)該直接創(chuàng)建偏好設(shè)置文件,而是應(yīng)該使用NSUserDefaults類來取得和設(shè)置應(yīng)用程序的偏好.
Caches 目錄:用于存放應(yīng)用程序?qū)S玫闹С治募4鎽?yīng)用程序再次啟動(dòng)過程中需要的信息。
4、tmp 目錄:這個(gè)目錄用于存放臨時(shí)文件,保存應(yīng)用程序再次啟動(dòng)過程中不需要的信息。
獲取這些目錄路徑的方法:
1,獲取家目錄路徑的函數(shù):
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,獲取應(yīng)用程序程序包中資源文件路徑的方法:
例如獲取程序包中一個(gè)圖片資源(apple.png)路徑的方法:
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@”apple” ofType:@”png”];
UIImage *appleImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
代碼中的mainBundle類方法用于返回一個(gè)代表應(yīng)用程序包的對(duì)象。
iphone沙盒(sandbox)中的幾個(gè)目錄獲取方式:
[cpp]view plaincopyprint?
// 獲取沙盒主目錄路徑
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();
[cpp]view plaincopyprint?
// 獲取當(dāng)前程序包中一個(gè)圖片資源(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]]){
//下面是對(duì)該文件進(jìn)行制定路徑的保存
[fm createDirectoryAtPath:[self dataFilePath] withIntermediateDirectories:YES attributes:nil error:nil];
//取得一個(gè)目錄下得所有文件名
NSArray *files = [fm subpathsAtPath: [self dataFilePath] ];
//讀取某個(gè)文件
NSData *data = [fm contentsAtPath:[self dataFilePath]];
//或者
NSData *data = [NSData dataWithContentOfPath:[self dataFilePath]];
}
1、常見的NSFileManager文件方法
-(NSData *)contentsAtPath:path //從一個(gè)文件讀取數(shù)據(jù)
-(BOOL)createFileAtPath: path contents:(NSData *)data attributes:attr //向一個(gè)文件寫入數(shù)據(jù)
-(BOOL)removeItemAtPath:path error:err //刪除一個(gè)文件
-(BOOL)moveItemAtPath:from toPath:to error:err //重命名或者移動(dòng)一個(gè)文件(to不能是已存在的)
-(BOOL)copyItemAtPath:from toPath:to error:err //復(fù)制文件(to不能是已存在的)
-(BOOL)contentsEqualAtPath:path andPath:path2 //比較兩個(gè)文件的內(nèi)容
-(BOOL)fileExistAtPath:path //測(cè)試文件是否存在
-(BOOL)isReadableFileAtPath:path //測(cè)試文件是否存在,并且是否能執(zhí)行讀操作
-(BOOL)isWriteableFileAtPath:path //測(cè)試文件是否存在,并且是否能執(zhí)行寫操作
-(NSDictionary *)attributesOfItemAtPath:path error:err //獲取文件的屬性
-(BOOL)setAttributesOfItemAtPath:attr error:err //更改文件的屬性
2.使用目錄
-(NSString *)currentDirectoryPath //獲取當(dāng)前目錄
-(BOOL)changeCurrentDirectoryPath:path //更改當(dāng)前目錄
-(BOOL)copyItemAtPath:from toPath:to error:err //復(fù)制目錄結(jié)構(gòu)(to不能是已存在的)
-(BOOL)createDirectoryAtPath:path withIntermediateDirectories:(BOOL)flag attribute:attr //創(chuàng)建一個(gè)新目錄
-(BOOL)fileExistAtPath:path isDirectory:(BOOL*)flag //測(cè)試文件是不是目錄(flag中儲(chǔ)存結(jié)果YES/NO)
-(NSArray *)contentsOfDirectoryAtPath:path error:err //列出目錄內(nèi)容
-(NSDirectoryEnumerator *)enumeratorAtPath:path //枚舉目錄的內(nèi)容
-(BOOL)removeItemAtPath:path error:err //刪除空目錄
-(BOOL)moveItemAtPath:from toPath:to error:err //重命名或移動(dòng)一個(gè)目錄(to不能是已存在的)
3、常用路徑工具方法
+(NSString *)pathWithComponens:components //根據(jù)components中的元素構(gòu)造有效路徑
-(NSArray *)pathComponents //析構(gòu)路徑,獲得組成此路徑的各個(gè)部分
-(NSString *)lastPathComponent //提取路徑的最后一個(gè)組成部分
-(NSString *)pathExtension //從路徑的最后一個(gè)組成部分中提取其擴(kuò)展名
-(NSString *)stringByAppendingPathComponent:path //將path添加到現(xiàn)有路徑的末尾
-(NSString *)stringByAppendingPathExtension:ext //將指定的擴(kuò)展名添加到路徑的最后一個(gè)組成部分
-(NSString *)stringByDeletingLastPathComponent //刪除路徑的最后一個(gè)組成部分
-(NSString *)stringByDeletingPathExtension //從文件的最后一部分刪除擴(kuò)展名
-(NSString *)stringByExpandingTileInPath //將路徑中代字符擴(kuò)展成用戶主目錄(~)或指定用戶的主目錄(~user)
-(NSString *)stringByresolvingSymlinksInPath //嘗試解析路徑中的符號(hào)鏈接
-(NSString *)stringByStandardizingPath //通過嘗試解析~、..(父目錄符號(hào))、.(當(dāng)前目錄符號(hào))和符號(hào)鏈接來標(biāo)準(zhǔn)化路徑
4、常用的路徑工具函數(shù)
NSString* NSUserName(void) //返回當(dāng)前用戶的登錄名
NSString* NSFullUserName(void) //返回當(dāng)前用戶的完整用戶名
NSString* NSHomeDirectory(void) //返回當(dāng)前用戶主目錄的路徑
NSString* NSHomeDirectoryForUser(NSString* user) //返回用戶user的主目錄
NSString* NSTemporaryDirectory(void) //返回可用于創(chuàng)建臨時(shí)文件的路徑目錄
5、常用的IOS目錄
Documents(NSDocumentDirectory) //用于寫入應(yīng)用相關(guān)數(shù)據(jù)文件的目錄,在ios中寫入這里的文件能夠與iTunes共享并訪問,存儲(chǔ)在這里的文件會(huì)自動(dòng)備份到云端
Library/Caches(NSCachesDirectory) //用于寫入應(yīng)用支持文件的目錄,保存應(yīng)用程序再次啟動(dòng)需要的信息。iTunes不會(huì)對(duì)這個(gè)目錄的內(nèi)容進(jìn)行備份
tmp(use NSTemporaryDirectory()) //這個(gè)目錄用于存放臨時(shí)文件,只程序終止時(shí)需要移除這些文件,當(dāng)應(yīng)用程序不再需要這些臨時(shí)文件時(shí),應(yīng)該將其從這個(gè)目錄中刪除
Library/Preferences //這個(gè)目錄包含應(yīng)用程序的偏好設(shè)置文件,使用 NSUserDefault類進(jìn)行偏好設(shè)置文件的創(chuàng)建、讀取和修改