一、獲取沙盒下文件目錄
沙盒應用根目錄:NSHomeDirectory()是應用程序目錄的路徑,在改文件目錄下有三個文件夾:Documents、Library、temp以及一個.app包
NSString *homeDir = NSHomeDirectory();
應用程序包:存放應用程序的源文件播急,包括資源文件和可執(zhí)行文件
[[NSBundle mainBundle] pathForResource:@"wtdb" ofType:@"sqlite"];
Documents:保存用戶生成的文件、應用程序不能重新創(chuàng)建的文件售睹∽可被iTunes備份
// 獲取Documents目錄路徑
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
Library:可創(chuàng)建子文件夾。除Caches以外昌妹,都會被iTunes備份
/Preferences:包含應用程序的偏好設置文件
/Caches:用于存放應用程序專用的支持文件捶枢,保存應用程序再次啟動過程中需要的信息握截,保存可以重新下載或者重新生成的數據。適合存儲體積大烂叔,不需要備份的非重要數據
// 獲取Library的目錄路徑
NSString *libDir = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
// 獲取Caches目錄路徑
NSString *cachesDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
tmp:存放臨時數據谨胞,保存應用程序再次啟動過程中不需要的信息。不會被iTunes備份
// 獲取tmp目錄路徑
NSString *tmpDir = NSTemporaryDirectory();
關于iOS Data Storage Guidelines蒜鸡,參考iOS Data Storage Guidelines - Apple Developer
-
Documents和Caches文件夾區(qū)別
如果你做個記事本的app胯努,那么用戶寫了東西,總要把東西存起來逢防。那么這個文件則是用戶自行生成的叶沛,就放在documents文件夾里面。
如果需要和服務器配合忘朝,經常從服務器下載東西灰署,展示給用戶看。那么這些下載下來的東西就放在library/caches局嘁。
二溉箕、文件夾操作
創(chuàng)建文件夾
NSString *documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//test文件夾
documentsDir = [documentsDir stringByAppendingPathComponent:@"test"];
//是否是文件夾
BOOL isDir;
BOOL isExit = [filemanager fileExistsAtPath:documentsDir isDirectory:&isDir];
//文件夾是否存在
if (!isExit || !isDir) {
[filemanager createDirectoryAtPath:documentsDir withIntermediateDirectories:YES attributes:nil error:nil];
}
刪除文件夾
//刪除Wtdb文件夾
NSString *cachesDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSString *wtdbPath = [cachesDir stringByAppendingPathComponent:@"Wtdb"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:wtdbPath]) {
BOOL isSuccess = [fileManager removeItemAtPath:wtdbPath error:nil];
}
移動文件夾
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];
NSString *moveToPath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];
BOOL isSuccess = [fileManager moveItemAtPath:filePath toPath:moveToPath error:nil];
if (isSuccess) {
NSLog(@"rename success");
}else{
NSLog(@"rename fail");
}
重命名文件夾
//通過移動該文件對文件重命名
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];
NSString *moveToPath = [documentsPath stringByAppendingPathComponent:@"rename.txt"];
BOOL isSuccess = [fileManager moveItemAtPath:filePath toPath:moveToPath error:nil];
if (isSuccess) {
NSLog(@"rename success");
}else{
NSLog(@"rename fail");
}
三、文件操作
復制
//bundle里的數據庫文件復制到Caches/Wtdb文件夾下
NSString *cachesDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSFileManager *filemanager = [NSFileManager defaultManager];
cachesDir = [cachesDir stringByAppendingPathComponent:@"Wtdb"];
BOOL isDir;//是否是文件夾
BOOL exit = [filemanager fileExistsAtPath:cachesDir isDirectory:&isDir];
//文件夾是否存在
if (!exit || !isDir) {
[filemanager createDirectoryAtPath:cachesDir withIntermediateDirectories:YES attributes:nil error:nil];
}
//判斷數據庫文件是否存在
NSString *wtdbPath = [cachesDir stringByAppendingPathComponent:@"wtdb.sqlite"];
//如果文件不存在悦昵,則復制
if (![filemanager fileExistsAtPath:wtdbPath]) {
NSString *dbBundlePath = [[NSBundle mainBundle] pathForResource:@"wtdb" ofType:@"sqlite"];
BOOL isSuccess = [filemanager copyItemAtPath:dbBundlePath toPath:wtdbPath error:nil];
DLog(@"數據庫文件%@", isSuccess ? @"拷貝成功" : @"拷貝失敗");
}
- 刪除肴茄、移動、重命名與文件夾操作類似旱捧,path為文件路徑独郎。
寫數據到文件
NSString *documentsPath =[self dirDoc];
NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
NSString *content=@"測試寫入內容!";
BOOL res=[content writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
if (res) {
NSLog(@"文件寫入成功");
}else {
NSLog(@"文件寫入失敗");
}
讀文件數據
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);
文件屬性
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);
}
參考: