iOS的沙盒機(jī)制,應(yīng)用只能訪問自己應(yīng)用目錄下的文件奠滑。iOS不像Android丹皱,沒有SD卡概念,不能直接訪問圖像宋税、視頻等內(nèi)容摊崭。iOS應(yīng)用產(chǎn)生的內(nèi)容,如圖像弃甥、文件爽室、緩存內(nèi)容等都必須存儲(chǔ)在自己的沙盒內(nèi)。默認(rèn)情況下淆攻,每個(gè)沙盒含有3個(gè)文件夾:Documents, Library 和 tmp阔墩。Library包含Caches、Preferences目錄
Documents:蘋果建議將程序創(chuàng)建產(chǎn)生的文件以及應(yīng)用瀏覽產(chǎn)生的文件數(shù)據(jù)保存在該目錄下瓶珊,iTunes備份和恢復(fù)的時(shí)候會(huì)包括此目錄
Library:存儲(chǔ)程序的默認(rèn)設(shè)置或其它狀態(tài)信息啸箫;
Library/Caches:存放緩存文件,保存應(yīng)用的持久化數(shù)據(jù)伞芹,用于應(yīng)用升級或者應(yīng)用關(guān)閉后的數(shù)據(jù)保存忘苛,不會(huì)被itunes同步蝉娜,所以為了減少同步的時(shí)間,可以考慮將一些比較大的文件而又不需要備份的文件放到這個(gè)目錄下扎唾。
tmp:提供一個(gè)即時(shí)創(chuàng)建臨時(shí)文件的地方召川,但不需要持久化,在應(yīng)用關(guān)閉后胸遇,該目錄下的數(shù)據(jù)將刪除荧呐,也可能系統(tǒng)在程序不運(yùn)行的時(shí)候清除。
APP? Sandbox
iOS怎么獲取沙盒路徑纸镊,怎么操作文件呢倍阐?下面給出答案。
獲取應(yīng)用沙盒根路徑:
-(void)dirHome
{
? NSString *dirHome=NSHomeDirectory();
? NSLog(@"app_home: %@",dirHome);
}
獲取Documents目錄路徑:
-(NSString *)dirDoc{
//[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"app_home_doc: %@",documentsDirectory);
return documentsDirectory;
}
獲取Library目錄
-(void)dirLib{
//[NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryDirectory = [paths objectAtIndex:0];
NSLog(@"app_home_lib: %@",libraryDirectory);
}
獲取Cache目錄路徑:
-(void)dirCache{
NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [cacPath objectAtIndex:0];
NSLog(@"app_home_lib_cache: %@",cachePath);
}
獲取Tmp目錄路徑:
-(void)dirTmp{
//[NSHomeDirectory() stringByAppendingPathComponent:@"tmp"];
NSString *tmpDirectory = NSTemporaryDirectory();
NSLog(@"app_home_tmp: %@",tmpDirectory);
}
創(chuàng)建文件夾:
-(void *)createDir{
NSString *documentsPath =[self dirDoc];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
// 創(chuàng)建目錄
BOOL res=[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];
if (res) {
NSLog(@"文件夾創(chuàng)建成功");
}else
NSLog(@"文件夾創(chuàng)建失敗");
}
創(chuàng)建文件
-(void *)createFile{
NSString *documentsPath =[self dirDoc];
NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
BOOL res=[fileManager createFileAtPath:testPath contents:nil attributes:nil];
if (res) {
NSLog(@"文件創(chuàng)建成功: %@" ,testPath);
}else
NSLog(@"文件創(chuàng)建失敗");
}
寫數(shù)據(jù)到文件:
-(void)writeFile{
NSString *documentsPath =[self dirDoc];
NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
NSString *content=@"測試寫入內(nèi)容逗威!";
BOOL res=[content writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
if (res) {
NSLog(@"文件寫入成功");
}else
NSLog(@"文件寫入失敗");
}
讀文件數(shù)據(jù):
-(void)readFile{
NSString *documentsPath =[self dirDoc];
NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
//? ? NSData *data = [NSData dataWithContentsOfFile:testPath];
//? ? NSLog(@"文件讀取成功: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
NSString *content=[NSString stringWithContentsOfFile:testPath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"文件讀取成功: %@",content);
}
文件屬性:
-(void)fileAttriutes{
NSString *documentsPath =[self dirDoc];
NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:testPath error:nil];
NSArray *keys;
id key, value;
keys = [fileAttributes allKeys];
int count = [keys count];
for (int i = 0; i < count; i++)
{
key = [keys objectAtIndex: i];
value = [fileAttributes objectForKey: key];
NSLog (@"Key: %@ for value: %@", key, value);
}
}
刪除文件:
-(void)deleteFile{
NSString *documentsPath =[self dirDoc];
NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
BOOL res=[fileManager removeItemAtPath:testPath error:nil];
if (res) {
NSLog(@"文件刪除成功");
}else
NSLog(@"文件刪除失敗");
NSLog(@"文件是否存在: %@",[fileManager isExecutableFileAtPath:testPath]?@"YES":@"NO");
}