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