SangBox 讀取寫入文件的封裝

@interface SandBoxPaths : NSObject

// 主路徑

+ (NSString *) homePath;

// document 路徑

+ (NSString *) documentPath;

// library 路徑

+ (NSString *) libraryPath;

// caches 路徑

+ (NSString *) cachesPath;

// tmp 路徑

+ (NSString *) tmpPath;

// 從 bundle 下獲取資源文件

+ (id) sourceFromBundleWithName:(NSString *)name type:(NSString *)type;

// 讀取文件

+(id)readDataWithPath:(NSString *)path dataType:(NSString *)dataType;

// 寫入文件

+(void) dataWriteToFileWithName:(NSString *)name type:(NSString *)type data:(id)data;

@end


@implementation SandBoxPaths

// 得到沙盒主路徑

+ (NSString *) homePath{

return NSHomeDirectory();

}

// 得到 documents 路徑

+ (NSString *) documentPath{

return NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

}

// 得到 library 路徑

+ (NSString *) libraryPath{

return NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0];

}

// 得到 caches 路徑

+ (NSString *) cachesPath{

return NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];

}

// 得到 tmp 路徑

+ (NSString *) tmpPath{

return? NSTemporaryDirectory();

}

// 獲取 bundle 下的資源文件

+ (id)sourceFromBundleWithName:(NSString *)name type:(NSString *)type{

return [[NSBundle mainBundle]pathForResource:name ofType:type];

}

// 寫入文件

+(void) dataWriteToFileWithName:(NSString *)name type:(NSString *)type data:(id)data{

if ([data isKindOfClass:[NSString class]]) {

// 說明要寫入的數(shù)據(jù)類型為字符串類型

// 1、先創(chuàng)建要寫入的文件路徑? stringByAppendingPathComponent:專門用來拼接文件路徑,在拼接的時候,不用添加“/”

NSString *docuPath = [[SandBoxPaths documentPath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@",name,type]];

// 2、將數(shù)據(jù)寫入到創(chuàng)建好的路徑下

// file:要保存數(shù)據(jù)的文件路徑

// atomically:原子性。 YES:保持文件的原子性,在文件的寫入過程中,系統(tǒng)會創(chuàng)建一個臨時文件,當文件整個寫入完成之后,會將臨時文件的數(shù)據(jù)轉(zhuǎn)移到咋們要存儲數(shù)據(jù)的路徑下。NO,直接將數(shù)據(jù)寫入到存儲的路徑下

// encoding 編碼格式不匹配

// error 錯誤信息

BOOL isWriteSuccess = [data writeToFile:docuPath atomically:YES encoding:NSUTF8StringEncoding error:nil];

if (isWriteSuccess) {

NSLog(@"%@文件寫入成功",name);

}else{

NSLog(@"%@文件寫入失敗",name);

}

// 寫入的文件類型非字符串類型? 如果要寫入的數(shù)據(jù)為數(shù)組或者字典,那么數(shù)組或者字典的元素類型也必須是簡單數(shù)據(jù)類型

}else{

NSString *pathString = [[SandBoxPaths documentPath]stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@",name,type]];

//? ? ? ? BOOL isWriteSuccess = [data writeToFile:pathString options:NSDataWritingAtomic error:nil];

BOOL isWriteSuccess =? [data writeToFile:pathString atomically:YES];

if (isWriteSuccess) {

NSLog(@"%@文件寫入成功",name);

}else{

NSLog(@"%@寫入失敗",name);

}

}

}

// 從文件路徑下讀取數(shù)據(jù)<封裝>

+(id)readDataWithPath:(NSString *)path dataType:(NSString *)dataType{

// 從路徑下讀取數(shù)據(jù)

if ([dataType isEqualToString:@"NSString"]) {

// NSClassFromeString 將字符串類型轉(zhuǎn)化為 類名

// NSStringFromClass 將類名轉(zhuǎn)化為字符串

// 字符串類型的讀取

return [[NSClassFromString(dataType)alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

}else{

// 非字符串類型的讀取

return [[NSClassFromString(dataType)alloc]initWithContentsOfFile:path];

}

}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容