国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

iOS中NSFileManager文件常用操作整合

2019-11-09 13:51:48
字體:
供稿:網(wǎng)友
//獲取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];}//獲取應(yīng)用程序路徑+ (NSString *)getapplicationPath{ return NSHomeDirectory();}//獲取Cache路徑+ (NSString *)getCachePath{ NSArray *filePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); return [filePaths objectAtIndex:0];}//獲取Temp路徑+ (NSString *)getTempPath{ return NSTemporaryDirectory();}//判斷文件是否存在于某個(gè)路徑中+ (BOOL)fileIsExistOfPath:(NSString *)filePath{ BOOL flag = NO; NSFileManager *fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath:filePath]) { flag = YES; } else { flag = NO; } return flag;}//從某個(gè)路徑中移除文件+ (BOOL)removeFileOfPath:(NSString *)filePath{ BOOL flag = YES; NSFileManager *fileManage = [NSFileManager defaultManager]; if ([fileManage fileExistsAtPath:filePath]) { if (![fileManage removeItemAtPath:filePath error:nil]) { flag = NO; } } return flag;}//從URL路徑中移除文件- (BOOL)removeFileOfURL:(NSURL *)fileURL{ BOOL flag = YES; NSFileManager *fileManage = [NSFileManager defaultManager]; if ([fileManage fileExistsAtPath:fileURL.path]) { if (![fileManage removeItemAtURL:fileURL error:nil]) { flag = NO; } } return flag;}//創(chuàng)建文件路徑+(BOOL)creatDirectoryWithPath:(NSString *)dirPath{ BOOL ret = YES; BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:dirPath]; if (!isExist) { NSError *error; BOOL isSuccess = [[NSFileManager defaultManager] createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:&error]; if (!isSuccess) { ret = NO; NSLog(@"creat Directory Failed. errorInfo:%@",error); } } return ret;}//創(chuàng)建文件+ (BOOL)creatFileWithPath:(NSString *)filePath{ BOOL isSuccess = YES; NSFileManager *fileManager = [NSFileManager defaultManager]; BOOL temp = [fileManager fileExistsAtPath:filePath]; if (temp) { return YES; } NSError *error; //stringByDeletingLastPathComponent:刪除最后一個(gè)路徑節(jié)點(diǎn) NSString *dirPath = [filePath stringByDeletingLastPathComponent]; isSuccess = [fileManager createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:&error]; if (error) { NSLog(@"creat File Failed. errorInfo:%@",error); } if (!isSuccess) { return isSuccess; } isSuccess = [fileManager createFileAtPath:filePath contents:nil attributes:nil]; return isSuccess;}//保存文件+ (BOOL)saveFile:(NSString *)filePath withData:(NSData *)data{ BOOL ret = YES; ret = [self creatFileWithPath:filePath]; if (ret) { ret = [data writeToFile:filePath atomically:YES]; if (!ret) { NSLog(@"%s Failed",__FUNCTION__); } } else { NSLog(@"%s Failed",__FUNCTION__); } return ret;}//追加寫文件+ (BOOL)appendData:(NSData *)data withPath:(NSString *)path{ BOOL result = [self creatFileWithPath:path]; if (result) { NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:path]; [handle seekToEndOfFile]; [handle writeData:data]; [handle synchronizeFile]; [handle closeFile]; return YES; } else { NSLog(@"%s Failed",__FUNCTION__); return NO; }}//獲取文件+ (NSData *)getFileData:(NSString *)filePath{ NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:filePath]; NSData *fileData = [handle readDataToEndOfFile]; [handle closeFile]; return fileData;}//讀取文件+ (NSData *)getFileData:(NSString *)filePath startIndex:(long long)startIndex length:(NSInteger)length{ NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:filePath]; [handle seekToFileOffset:startIndex]; NSData *data = [handle readDataOfLength:length]; [handle closeFile]; return data;}//移動(dòng)文件+ (BOOL)moveFileFromPath:(NSString *)fromPath toPath:(NSString *)toPath{ NSFileManager *fileManager = [NSFileManager defaultManager]; if (![fileManager fileExistsAtPath:fromPath]) { NSLog(@"Error: fromPath Not Exist"); return NO; } if (![fileManager fileExistsAtPath:toPath]) { NSLog(@"Error: toPath Not Exist"); return NO; } NSString *headerComponent = [toPath stringByDeletingLastPathComponent]; if ([self creatFileWithPath:headerComponent]) { return [fileManager moveItemAtPath:fromPath toPath:toPath error:nil]; } else { return NO; }}//拷貝文件+(BOOL)copyFileFromPath:(NSString *)fromPath toPath:(NSString *)toPath{ NSFileManager *fileManager = [NSFileManager defaultManager]; if (![fileManager fileExistsAtPath:fromPath]) { NSLog(@"Error: fromPath Not Exist"); return NO; } if (![fileManager fileExistsAtPath:toPath]) { NSLog(@"Error: toPath Not Exist"); return NO; } NSString *headerComponent = [toPath stringByDeletingLastPathComponent]; if ([self creatFileWithPath:headerComponent]) { return [fileManager copyItemAtPath:fromPath toPath:toPath error:nil]; } else { return NO; }}//獲取文件夾下文件列表+ (NSArray *)getFileListInFolderWithPath:(NSString *)path{ NSFileManager *fileManager = [NSFileManager defaultManager]; NSError *error; NSArray *fileList = [fileManager contentsOfDirectoryAtPath:path error:&error]; if (error) { NSLog(@"getFileListInFolderWithPathFailed, errorInfo:%@",error); } return fileList;}//獲取文件大小+ (long long)getFileSizeWithPath:(NSString *)path{ unsigned long long fileLength = 0; NSNumber *fileSize; NSFileManager *fileManager = [NSFileManager defaultManager]; 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;}//獲取文件創(chuàng)建時(shí)間+ (NSString *)getFileCreatDateWithPath:(NSString *)path{ NSString *date = nil; NSFileManager *fileManager = [NSFileManager defaultManager]; NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:path error:nil]; date = [fileAttributes objectForKey:NSFileCreationDate]; return date;}//獲取文件所有者+ (NSString *)getFileOwnerWithPath:(NSString *)path{ NSString *fileOwner = nil; NSFileManager *fileManager = [NSFileManager defaultManager]; NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:path error:nil]; fileOwner = [fileAttributes objectForKey:NSFileOwnerAccountName]; return fileOwner;}//獲取文件更改日期+ (NSString *)getFileChangeDateWithPath:(NSString *)path{ NSString *date = nil; NSFileManager *fileManager = [NSFileManager defaultManager]; NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:path error:nil]; date = [fileAttributes objectForKey:NSFileModificationDate]; return date;}

工程代碼下載地址(更多常用操作整合)


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宝坻区| 虎林市| 小金县| 丹凤县| 应城市| 西宁市| 达拉特旗| 汾阳市| 桃园县| 进贤县| 启东市| 贡嘎县| 乐亭县| 乌拉特中旗| 星座| 石棉县| 喜德县| 丰城市| 星子县| 贵德县| 遵义县| 宁陵县| 昌图县| 潞西市| 榆林市| 遵义县| 四川省| 新丰县| 通州市| 水城县| 霸州市| 江津市| 红河县| 勐海县| 牡丹江市| 青神县| 额济纳旗| 巴彦淖尔市| 唐河县| 两当县| 凤台县|