? ? ? ?學習了數據存儲的NSUserDefaults,歸檔和解檔,對于項目開發中如果要存儲一些文件,比如圖片,音頻,視頻等文件的時候就需要用到文件存儲了。文件沙盒存儲主要存儲非機密數據,大的數據。
關于沙盒:
每個ios應用都有自己的應用沙盒,應用沙盒就是文件系統目錄,與其他應用的文件系統隔離,ios系統不允許訪問其他應用的應用沙盒。在ios8中已經開放訪問。
應用沙盒一般包括以下幾個文件目錄:應用程序包、Documents、Libaray(下面有Caches和Preferences目錄)、tmp。
應用程序包:包含所有的資源文件和可執行文件。
Documents:保存應用運行時生成的需要持久化的數據,iTunes會自動備份該目錄。蘋果建議將程序中建立的或在程序中瀏覽到的文件數據保存在該目錄下,iTunes備份和恢復的時候會包括此目錄
tmp:保存應用運行時所需的臨時數據,使用完畢后再將相應的文件從該目錄刪除。應用沒有運行時,系統也有可能會清除該目錄下的文件,iTunes不會同步該目錄。iphone重啟時,該目錄下的文件會丟失。
Library:存儲程序的默認設置和其他狀態信息,iTunes會自動備份該目錄。
Libaray/Caches:存放緩存文件,iTunes不會備份此目錄,此目錄下文件不會在應用退出刪除。一般存放體積比較大,不是特別重要的資源。
Libaray/Preferences:保存應用的所有偏好設置,ios的Settings(設置)應用會在該目錄中查找應用的設置信息,iTunes會自動備份該目錄。
具體獲取各個目錄代碼如下:
// 獲得應用程序沙盒的Documents文件夾路徑
NSArray *arrDocumentPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentPath=[arrDocumentPaths objectAtIndex:0];
NSLog(@"Documents path: %@",documentPath);
// 獲得應用程序沙盒的Caches文件夾路徑
NSArray *arrCachesPaths=NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);
NSString *CachesPath=[arrCachesPaths objectAtIndex:0];
NSLog(@"Caches path: %@",CachesPath);
// 獲得應用程序沙盒的Downloads文件夾路徑
NSArray *arrDownloadPaths=NSSearchPathForDirectoriesInDomains(NSDownloadsDirectory,NSUserDomainMask,YES);
NSString *loadPathsPath=[arrDownloadPaths objectAtIndex:0];
NSLog(@"Downloads path: %@",loadPathsPath);
// 獲得應用程序沙盒的home文件夾路徑
NSString *homePath= NSHomeDirectory();
// 獲得應用程序沙盒的tmp文件夾路徑
NSString *TmpPath= NSTemporaryDirectory();
為了方便使用整理一個File工具類:
FileUtils.h
#import@interface FileUtils : NSObject
//返回緩存根目錄 "caches"
+(NSString *)getCachesDirectory;
//返回根目錄路徑 "document"
+ (NSString *)getDocumentPath;
//創建文件夾
+(BOOL)creatDir:(NSString*)dirPath;
//刪除文件夾
+(BOOL)deleteDir:(NSString*)dirPath;
//移動文件夾
+(BOOL)moveDir:(NSString*)srcPath to:(NSString*)desPath;
//創建文件
+ (BOOL)creatFile:(NSString*)filePath withData:(NSData*)data;
//讀取文件
+(NSData*)readFile:(NSString *)filePath;
//刪除文件
+(BOOL)deleteFile:(NSString *)filePath;
//返回 文件全路徑
+ (NSString*)getFilePath:(NSString*) fileName;
//在對應文件保存數據
+ (BOOL)writeDataToFile:(NSString*)fileName data:(NSData*)data;
//從對應的文件讀取數據
+ (NSData*)readDataFromFile:(NSString*)fileName;
@end
FileUtils.m
#import "FileUtils.h"
@implementation FileUtils
//返回緩存根目錄 "caches"
+(NSString *)getCachesDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *caches = [paths firstObject];
return caches;
}
//返回根目錄路徑 "document"
+ (NSString *)getDocumentPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [paths firstObject];
return documentPath;
}
//創建文件目錄
+(BOOL)creatDir:(NSString*)dirPath
{
if ([[NSFileManager defaultManager] fileExistsAtPath:dirPath])//判斷dirPath路徑文件夾是否已存在,此處dirPath為需要新建的文件夾的絕對路徑
{
return NO;
}
else
{
[[NSFileManager defaultManager] createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:nil];//創建文件夾
return YES;
}
}
//刪除文件目錄
+(BOOL)deleteDir:(NSString*)dirPath
{
if([[NSFileManager defaultManager] fileExistsAtPath:dirPath])//如果存在臨時文件的配置文件
{
NSError *error=nil;
return [[NSFileManager defaultManager]? removeItemAtPath:dirPath error:&error];
}
return? NO;
}
//移動文件夾
+(BOOL)moveDir:(NSString*)srcPath to:(NSString*)desPath;
{
NSError *error=nil;
if([[NSFileManager defaultManager] moveItemAtPath:srcPath toPath:desPath error:&error]!=YES)// prePath 為原路徑、? ? cenPath 為目標路徑
{
NSLog(@"移動文件失敗");
return NO;
}
else
{
NSLog(@"移動文件成功");
return YES;
}
}
//創建文件
+ (BOOL)creatFile:(NSString*)filePath withData:(NSData*)data
{
return? [[NSFileManager defaultManager] createFileAtPath:filePath contents:data attributes:nil];
}
//讀取文件
+(NSData*)readFile:(NSString *)filePath
{
return [NSData dataWithContentsOfFile:filePath options:0 error:NULL];
}
//刪除文件
+(BOOL)deleteFile:(NSString *)filePath
{
return [self deleteDir:filePath];
}
+ (NSString *)getFilePath:(NSString *)fileName
{
NSString *dirPath = [[self getDocumentPath] stringByAppendingPathComponent:fileName];
return dirPath;
}
+ (BOOL)writeDataToFile:(NSString*)fileName data:(NSData*)data
{
NSString *filePath=[self getFilePath:fileName];
return [self creatFile:filePath withData:data];
}
+ (NSData*)readDataFromFile:(NSString*)fileName
{
NSString *filePath=[self getFilePath:fileName];
return [self readFile:filePath];
}
@end