iphone沙箱模型的有四個文件夾鸯两,分別是什么闷旧,永久數(shù)據(jù)存儲一般放在什么位置,得到模擬器的路徑的簡單方式是什么.
documents钧唐,tmp忙灼,app,Library钝侠。
(NSHomeDirectory())该园,
手動保存的文件在documents文件里
Nsuserdefaults保存的文件在tmp文件夾里
1、Documents 目錄:您應(yīng)該將所有de應(yīng)用程序數(shù)據(jù)文件寫入到這個目錄下帅韧。這個目錄用于存儲用戶數(shù)據(jù)或其它應(yīng)該定期備份的信息里初。
2、AppName.app 目錄:這是應(yīng)用程序的程序包目錄忽舟,包含應(yīng)用程序的本身双妨。由于應(yīng)用程序必須經(jīng)過簽名淮阐,所以您在運行時不能對這個目錄中的內(nèi)容進(jìn)行修改,否則可能會使應(yīng)用程序無法啟動斥难。
3、Library 目錄:這個目錄下有兩個子目錄: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)用程序再次啟動過程中需要的信息及刻。
4镀裤、tmp 目錄:這個目錄用于存放臨時文件,保存應(yī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)用程序程序包中資源文件路徑的方法:
例如獲取程序包中一個圖片資源(apple.png)路徑的方法:
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@”apple” ofType:@”png”];
UIImage *appleImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
代碼中的mainBundle類方法用于返回一個代表應(yīng)用程序包的對象丢氢。
iphone沙盒(sandbox)中的幾個目錄獲取方式:
[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)前程序包中一個圖片資源(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]]){
//下面是對該文件進(jìn)行制定路徑的保存
[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 //從一個文件讀取數(shù)據(jù)
-(BOOL)createFileAtPath: path contents:(NSData *)data attributes:attr //向一個文件寫入數(shù)據(jù)
-(BOOL)removeItemAtPath:path error:err //刪除一個文件
-(BOOL)moveItemAtPath:from toPath:to error:err //重命名或者移動一個文件(to不能是已存在的)
-(BOOL)copyItemAtPath:from toPath:to error:err //復(fù)制文件(to不能是已存在的)
-(BOOL)contentsEqualAtPath:path andPath:path2 //比較兩個文件的內(nèi)容
-(BOOL)fileExistAtPath:path //測試文件是否存在
-(BOOL)isReadableFileAtPath:path //測試文件是否存在,并且是否能執(zhí)行讀操作
-(BOOL)isWriteableFileAtPath:path //測試文件是否存在蒸走,并且是否能執(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)建一個新目錄
-(BOOL)fileExistAtPath:path isDirectory:(BOOL*)flag //測試文件是不是目錄(flag中儲存結(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 //重命名或移動一個目錄(to不能是已存在的)
3、常用路徑工具方法
+(NSString *)pathWithComponens:components //根據(jù)components中的元素構(gòu)造有效路徑
-(NSArray *)pathComponents //析構(gòu)路徑比驻,獲得組成此路徑的各個部分
-(NSString *)lastPathComponent //提取路徑的最后一個組成部分
-(NSString *)pathExtension //從路徑的最后一個組成部分中提取其擴展名
-(NSString *)stringByAppendingPathComponent:path //將path添加到現(xiàn)有路徑的末尾
-(NSString *)stringByAppendingPathExtension:ext //將指定的擴展名添加到路徑的最后一個組成部分
-(NSString *)stringByDeletingLastPathComponent //刪除路徑的最后一個組成部分
-(NSString *)stringByDeletingPathExtension //從文件的最后一部分刪除擴展名
-(NSString *)stringByExpandingTileInPath //將路徑中代字符擴展成用戶主目錄(~)或指定用戶的主目錄(~user)
-(NSString *)stringByresolvingSymlinksInPath //嘗試解析路徑中的符號鏈接
-(NSString *)stringByStandardizingPath //通過嘗試解析~、..(父目錄符號)别惦、.(當(dāng)前目錄符號)和符號鏈接來標(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)建臨時文件的路徑目錄
5夫椭、常用的IOS目錄
Documents(NSDocumentDirectory) //用于寫入應(yīng)用相關(guān)數(shù)據(jù)文件的目錄步咪,在ios中寫入這里的文件能夠與iTunes共享并訪問,存儲在這里的文件會自動備份到云端
Library/Caches(NSCachesDirectory) //用于寫入應(yīng)用支持文件的目錄益楼,保存應(yīng)用程序再次啟動需要的信息。iTunes不會對這個目錄的內(nèi)容進(jìn)行備份
tmp(use NSTemporaryDirectory()) //這個目錄用于存放臨時文件悯周,只程序終止時需要移除這些文件,當(dāng)應(yīng)用程序不再需要這些臨時文件時陪竿,應(yīng)該將其從這個目錄中刪除
Library/Preferences //這個目錄包含應(yīng)用程序的偏好設(shè)置文件禽翼,使用 NSUserDefault類進(jìn)行偏好設(shè)置文件的創(chuàng)建屠橄、讀取和修改