iOS中NSFileManager文件常用操作整合

原文出處 http://blog.csdn.net/feng2qing/article/details/54974200

//獲取Document路徑

+ (NSString*)getDocumentPath

{NSArray*filePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);return[filePaths objectAtIndex:0];

}

//獲取Library路徑

+ (NSString*)getLibraryPath

{NSArray*filePaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask,YES);return[filePaths objectAtIndex:0];

}

//獲取應用程序路徑+ (NSString*)getApplicationPath

{returnNSHomeDirectory();

}

//獲取Cache路徑

+ (NSString*)getCachePath

{NSArray*filePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,YES);return[filePaths objectAtIndex:0];

}

//獲取Temp路徑+ (NSString*)getTempPath

{returnNSTemporaryDirectory();

}

//判斷文件是否存在于某個路徑中

+ (BOOL)fileIsExistOfPath:(NSString*)filePath

{BOOLflag =NO;NSFileManager*fileManager = [NSFileManagerdefaultManager];if([fileManager fileExistsAtPath:filePath]) {

flag =YES;

}else{

flag =NO;

}returnflag;

}

//從某個路徑中移除文件

+ (BOOL)removeFileOfPath:(NSString*)filePath

{BOOLflag =YES;NSFileManager*fileManage = [NSFileManagerdefaultManager];if([fileManage fileExistsAtPath:filePath]) {if(![fileManage removeItemAtPath:filePath error:nil]) {

flag =NO;

}

}returnflag;

}

//從URL路徑中移除文件

- (BOOL)removeFileOfURL:(NSURL*)fileURL

{BOOLflag =YES;NSFileManager*fileManage = [NSFileManagerdefaultManager];if([fileManage fileExistsAtPath:fileURL.path]) {if(![fileManage removeItemAtURL:fileURL error:nil]) {

flag =NO;

}

}returnflag;

}

//創建文件路徑

+(BOOL)creatDirectoryWithPath:(NSString*)dirPath

{BOOLret =YES;BOOLisExist = [[NSFileManagerdefaultManager] fileExistsAtPath:dirPath];if(!isExist) {NSError*error;BOOLisSuccess = [[NSFileManagerdefaultManager] createDirectoryAtPath:dirPath withIntermediateDirectories:YESattributes:nilerror:&error];if(!isSuccess) {

ret =NO;NSLog(@"creat Directory Failed. errorInfo:%@",error);

}

}returnret;

}

//創建文件

+ (BOOL)creatFileWithPath:(NSString*)filePath

{BOOLisSuccess =YES;NSFileManager*fileManager = [NSFileManagerdefaultManager];BOOLtemp = [fileManager fileExistsAtPath:filePath];if(temp) {returnYES;

}NSError*error;//stringByDeletingLastPathComponent:刪除最后一個路徑節點NSString*dirPath = [filePath stringByDeletingLastPathComponent];

isSuccess = [fileManager createDirectoryAtPath:dirPath withIntermediateDirectories:YESattributes:nilerror:&error];if(error) {NSLog(@"creat File Failed. errorInfo:%@",error);

}if(!isSuccess) {returnisSuccess;

}

isSuccess = [fileManager createFileAtPath:filePath contents:nilattributes:nil];returnisSuccess;

}

//保存文件

+ (BOOL)saveFile:(NSString*)filePath withData:(NSData *)data

{BOOLret =YES;

ret = [selfcreatFileWithPath:filePath];if(ret) {

ret = [data writeToFile:filePath atomically:YES];if(!ret) {NSLog(@"%s Failed",__FUNCTION__);

}

}else{NSLog(@"%s Failed",__FUNCTION__);

}returnret;

}//追加寫文件+ (BOOL)appendData:(NSData *)data withPath:(NSString*)path

{BOOLresult = [selfcreatFileWithPath:path];if(result) {

NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:path];

[handle seekToEndOfFile];

[handle writeData:data];

[handle synchronizeFile];

[handle closeFile];returnYES;

}else{NSLog(@"%s Failed",__FUNCTION__);returnNO;

}

}

//獲取文件

+ (NSData *)getFileData:(NSString*)filePath

{

NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:filePath];

NSData *fileData = [handle readDataToEndOfFile];

[handle closeFile];returnfileData;

}

//讀取文件

+ (NSData *)getFileData:(NSString*)filePath startIndex:(longlong)startIndex length:(NSInteger)length

{

NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:filePath];

[handle seekToFileOffset:startIndex];

NSData *data = [handle readDataOfLength:length];

[handle closeFile];returndata;

}

//移動文件

+ (BOOL)moveFileFromPath:(NSString*)fromPath toPath:(NSString*)toPath

{NSFileManager*fileManager = [NSFileManagerdefaultManager];if(![fileManager fileExistsAtPath:fromPath]) {NSLog(@"Error: fromPath Not Exist");returnNO;

}if(![fileManager fileExistsAtPath:toPath]) {NSLog(@"Error: toPath Not Exist");returnNO;

}NSString*headerComponent = [toPath stringByDeletingLastPathComponent];if([selfcreatFileWithPath:headerComponent]) {return[fileManager moveItemAtPath:fromPath toPath:toPath error:nil];

}else{returnNO;

}

}

//拷貝文件

+(BOOL)copyFileFromPath:(NSString*)fromPath toPath:(NSString*)toPath

{NSFileManager*fileManager = [NSFileManagerdefaultManager];if(![fileManager fileExistsAtPath:fromPath]) {NSLog(@"Error: fromPath Not Exist");returnNO;

}if(![fileManager fileExistsAtPath:toPath]) {NSLog(@"Error: toPath Not Exist");returnNO;

}NSString*headerComponent = [toPath stringByDeletingLastPathComponent];if([selfcreatFileWithPath:headerComponent]) {return[fileManager copyItemAtPath:fromPath toPath:toPath error:nil];

}else{returnNO;

}

}

//獲取文件夾下文件列表

+ (NSArray*)getFileListInFolderWithPath:(NSString*)path

{NSFileManager*fileManager = [NSFileManagerdefaultManager];NSError*error;NSArray*fileList = [fileManager contentsOfDirectoryAtPath:path error:&error];if(error) {NSLog(@"getFileListInFolderWithPathFailed, errorInfo:%@",error);

}returnfileList;

}

//獲取文件大小

+ (longlong)getFileSizeWithPath:(NSString*)path

{

unsignedlonglongfileLength =0;

NSNumber*fileSize;

NSFileManager*fileManager = [NSFileManagerdefaultManager];NSDictionary*fileAttributes = [fileManager attributesOfItemAtPath:path error:nil];

if((fileSize = [fileAttributes objectForKey:NSFileSize])) {

fileLength = [fileSize unsignedLongLongValue];

return fileLength;

}

//

NSFileManager* manager =[NSFileManager defaultManager];

//? ?

?if ([manager fileExistsAtPath:path]){

//? ? ? ??

return [[manager attributesOfItemAtPath:path error:nil] fileSize];

//??

? }

//

? ? return 0;

}

//獲取文件創建時間

+ (NSString*)getFileCreatDateWithPath:(NSString*)path

{NSString*date =nil;NSFileManager*fileManager = [NSFileManagerdefaultManager];NSDictionary*fileAttributes = [fileManager attributesOfItemAtPath:path error:nil];

date = [fileAttributes objectForKey:NSFileCreationDate];returndate;

}

//獲取文件所有者

+ (NSString*)getFileOwnerWithPath:(NSString*)path

{NSString*fileOwner =nil;NSFileManager*fileManager = [NSFileManagerdefaultManager];NSDictionary*fileAttributes = [fileManager attributesOfItemAtPath:path error:nil];

fileOwner = [fileAttributes objectForKey:NSFileOwnerAccountName];returnfileOwner;

}

//獲取文件更改日期

+ (NSString*)getFileChangeDateWithPath:(NSString*)path

{NSString*date =nil;NSFileManager*fileManager = [NSFileManagerdefaultManager];NSDictionary*fileAttributes = [fileManager attributesOfItemAtPath:path error:nil];

date = [fileAttributes objectForKey:NSFileModificationDate];returndate;

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,238評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,430評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,134評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,893評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,653評論 6 408
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,136評論 1 323
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,212評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,372評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,888評論 1 334
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,738評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,939評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,482評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,179評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,588評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,829評論 1 283
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,610評論 3 391
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,916評論 2 372

推薦閱讀更多精彩內容

  • 一、iOS中的沙盒機制 ?iOS應用程序只能對自己創建的文件系統讀取文件,這個獨立、封閉、安全的空間,叫做沙盒。它...
    舒城8中閱讀 2,399評論 0 6
  • 一、iOS中的沙盒機制 iOS應用程序只能對自己創建的文件系統讀取文件,這個獨立、封閉、安全的空間,叫做沙盒。它一...
    絢雨藍了個楓閱讀 4,112評論 0 2
  • 27、ViewController的didReceiveMemoryWarning是在什么時候調用的?默認的操作是...
    煙雨平生花飛舞閱讀 601評論 0 1
  • 昨天一天下午還真是個種查,各種搜索,然后各種技術群,各種問,(沒人鳥我),其實我是有這個能力的,怎么就一上...
    夢隨興飛閱讀 25,114評論 7 18
  • 一、iOS中的沙盒機制 iOS應用程序只能對自己創建的文件系統讀取文件,這個獨立、封閉、安全的空間,叫做沙盒。它一...
    1d5cb7cff98d閱讀 1,783評論 0 0