NSFileManager+Category

2024-06-08 17:32
文章标签 category nsfilemanager

本文主要是介绍NSFileManager+Category,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.头文件

#import <Foundation/Foundation.h>#define USER_FOLDER @"/User"
#define USER_CACHE_FOLDER @"/Cache"
#define USER_LOG_FOLDER [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingString:@"/User/Log/"]#define DB_FILE_NAME @"HMC"@interface NSFileManager (Category)+ (NSString *)getDocumentDirectory;+ (void)createFolderInDirectory:(NSString *)directory folder:(NSString *)folder;+ (void)createFileDirectories:(NSString *)dictory;+ (void)copyItemToFolder:(NSString *)folderName fileName:(NSString *)fileName dataBasePath:(NSString *)dbPath;+ (BOOL)fileExistWithName:(NSString *)fileName ;+ (NSString *)getFileContent:(NSString *)fileName;+ (NSMutableArray *)getAllFileNamesInDirectory:(NSString*)directory;+ (BOOL)copyFileFromDirectory:(NSString*)fromDir toDirectory:(NSString*)toDir fileName:(NSString*)fileName;
@end


2.m文件

#import "NSFileManager+Category.h"@implementation NSFileManager (Category)+ (NSString *)getDocumentDirectory
{NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentsDirectory = [paths objectAtIndex:0];return documentsDirectory;
}+ (void)createFolderInDirectory:(NSString *)directory folder:(NSString *)folder
{NSString *folderPath = [NSString stringWithFormat:@"%@%@", directory, folder];NSFileManager *fileManager = [NSFileManager defaultManager];BOOL isDir = FALSE;BOOL isDirExist = [fileManager fileExistsAtPath:folderPath isDirectory:&isDir];if(!(isDirExist && isDir))    {BOOL bCreateDir = [fileManager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:nil];if(!bCreateDir){DDLogError(@"Create Mid Directory Failed.");}}}+ (void)createFileDirectories:(NSString *)dictory {    // 判断存放mid、mov的文件夹是否存在,不存在则创建对应文件夹NSString *midPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"db"];NSFileManager *fileManager = [NSFileManager defaultManager];BOOL isDir = FALSE;BOOL isDirExist = [fileManager fileExistsAtPath:midPath isDirectory:&isDir];if(!(isDirExist && isDir))    {BOOL bCreateDir = [fileManager createDirectoryAtPath:midPath withIntermediateDirectories:YES attributes:nil error:nil];if(!bCreateDir){DDLogError(@"Create Mid Directory Failed.");}DDLogDebug(@"%@",midPath);}NSString *movPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"mov"];isDir = FALSE;isDirExist = [fileManager fileExistsAtPath:movPath isDirectory:&isDir];if(!(isDirExist && isDir)){BOOL bCreateDir = [fileManager createDirectoryAtPath:movPath withIntermediateDirectories:YES attributes:nil error:nil];if(!bCreateDir){DDLogError(@"Create Mov Directory Failed.");}DDLogDebug(@"%@",movPath);}
}+ (void)copyDBItemToFolder:(NSString *)folderName
{}+ (void)copyItemToFolder:(NSString *)folderName fileName:(NSString *)fileName dataBasePath:(NSString *)dbPath
{NSFileManager *fileManager = [NSFileManager defaultManager];NSString *filePath = [[folderName stringByAppendingPathComponent:fileName] stringByAppendingString:@".db"];NSString * resourcePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"db"];if([fileManager fileExistsAtPath:filePath] == NO && filePath && resourcePath) {[fileManager copyItemAtPath:resourcePath toPath:filePath error:nil];}
}+ (BOOL)fileExistWithName:(NSString *)fileName {NSFileManager *fileManager = [NSFileManager defaultManager];NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentsDirectory = [paths objectAtIndex:0];NSString *filePath = [[documentsDirectory stringByAppendingPathComponent:@"db"] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.db", fileName]];return [fileManager fileExistsAtPath:filePath];
}+ (NSString *)getFileContent:(NSString *)fileName
{NSError *error=nil;NSString *str=[NSString stringWithContentsOfFile:fileName encoding:NSUTF8StringEncoding error:&error];return str;
}+ (NSMutableArray *)getAllFileNamesInDirectory:(NSString*)directory{NSMutableArray *retVal = [[NSMutableArray alloc] init];NSFileManager *nsFile = [NSFileManager defaultManager];NSError *err = nil;if ([nsFile fileExistsAtPath:directory]) {NSArray *sysList = [nsFile contentsOfDirectoryAtPath:directory error:&err];if (sysList && sysList.count > 0) {for (NSString *fileName in sysList) {if ([fileName isEqualToString: @"."] || [fileName isEqualToString:@".."] || [fileName isEqualToString:@".DS_Store"])continue;[retVal addObject:fileName];}}}else{if (![[NSFileManager defaultManager] createDirectoryAtPath:directorywithIntermediateDirectories:YESattributes:nilerror:&err]) {DDLogError(@"Error creating Directory!\nerror: %@\ndirectory: %@", err,directory);}else{DDLogInfo(@"Success creating Directory: %@",directory);}}return retVal;
}// 将文件转移到指定目录文件夹,先加.bak后重命名
+ (BOOL)copyFileFromDirectory:(NSString*)fromDir toDirectory:(NSString*)toDir fileName:(NSString*)fileName{NSString *filePath = [NSString stringWithFormat:@"%@/%@", fromDir,fileName];if([fromDir hasSuffix:@"/"]){filePath = [fromDir stringByAppendingString:fileName];}if (!fromDir || [fromDir isEqualToString:@""] ) {DDLogWarn(@"Warn fromDir is nil!");return NO;}if (!toDir || [toDir isEqualToString:@""] ) {DDLogWarn(@"Warn toDir is nil!");return NO;}if (![[NSFileManager defaultManager] fileExistsAtPath:filePath] ) {DDLogWarn(@"Warn fileName is no exist!");return NO;}DDLogDebug(@"fromDir:%@\ntoDir:%@\nfileName:%@\n",fromDir,toDir,fileName);NSError *err = nil;if (![[NSFileManager defaultManager] fileExistsAtPath:fromDir]) {if (![[NSFileManager defaultManager] createDirectoryAtPath:fromDirwithIntermediateDirectories:YESattributes:nilerror:&err]) {DDLogError(@"Error creating Directory!\nerror: %@\ndirectory: %@", err,fromDir);return NO;}else{DDLogInfo(@"Success creating Directory: %@",fromDir);}}if (![[NSFileManager defaultManager] fileExistsAtPath:toDir]) {if (![[NSFileManager defaultManager] createDirectoryAtPath:toDirwithIntermediateDirectories:YESattributes:nilerror:&err]) {DDLogError(@"Error creating Directory!\nerror: %@\ndirectory: %@", err,toDir);return NO;}else{DDLogInfo(@"Success creating Directory: %@",toDir);}}NSString *fileBakName = [NSString stringWithFormat:@"%@.bak",fileName];NSString *fileBakPath = [NSString stringWithFormat:@"%@/%@", toDir,fileBakName];if([toDir hasSuffix:@"/"]){fileBakPath = [toDir stringByAppendingString:fileBakName];}if (![[NSFileManager defaultManager] copyItemAtPath:filePath toPath:fileBakPath error:&err]) {DDLogError(@"Error copy file (%@): %@", fileBakName, err);return NO;}else{DDLogInfo(@"Success copy file (%@)",fileBakName);NSString *newFilePath = [NSString stringWithFormat:@"%@/%@", toDir,fileName];if([toDir hasSuffix:@"/"]){newFilePath = [toDir stringByAppendingString:fileName];}if (![[NSFileManager defaultManager] moveItemAtPath:fileBakPath toPath:newFilePath error:&err]) {DDLogError(@"Error renaming file (%@) to (%@) .\nerror : %@", fileBakName , fileName, err);return NO;}DDLogInfo(@"Success renaming file (%@) to (%@)", fileBakName, fileName);return YES;}
}@end


这篇关于NSFileManager+Category的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1042841

相关文章

AI模型:追求全能还是专精?-- 之6 语言复杂度类别(Category 0~3 类)和语言功能性类型(Type 0~Ⅲ 型)之2

Q17、我前面说过,语言复杂度的0~3级(Category 0~3)表示了语言的的上下文相关性 : 完全不相关, 单相关的 单词上下文, 双相关的句子上下文 全相关的文章上下文 。我准备翻译为 Context - irrelative /relative/correlative/ full-correlative,显式表达了语言复杂度的0~3级(Category 0~3)区别的上下文相关性是一种关

[iOS]iOS AudioSession详解 Category选择 听筒扬声器切换

在你读这篇文章之前,如果你不嫌读英文太累,推荐阅读下苹果iOS Human Interface Guidelines中Sound这一章。 选择一个Category AVAudioSessionCategoryAmbient 或 kAudioSessionCategory_AmbientSound ——用于非以语音为主的应用,使用这个category的应用会随着静音键和屏幕关

执行hadoop中bin操作时报错:Operation category READ is not supported in state standby

在hadoop执行操作的时候报错如下:   从报错中可以分析出错误:其中Operation category READ is not supported in state standby   原因:配置了Hadoop HA,启动了一个hadoop NameNode时而启动的NameNode为standby状态。   查看nn1状态:   解决方法:将standby状态手动改为act

OC 中的分类(Category)详解

1、Category  分类、类别 2、分类:可以给某一个类扩充一些方法(不修改原来类的代码) 3、分类也分为声明(放在.h文件)和实现(放在.m文件): 分类的声明格式如下 @interface 类名(分类名称) //要给类扩充的方法的声明 @end 分类的实现 @implementation  类名(分类名称) //对应的方法实现 @end 4、分类名一般以模块进行

Objective-c NSFileManager类和NSFileHandle类的使用

NSFileManager* fm = [NSFileManager defaultManager];         //判断一个文件是否存在,返回一个BOOL值         if([fm fileExistsAtPath:@"/tmp/AddressCard.m"])         {             NSLog(@"exist");         }

android.intent.category.DEFAULT的用途和使用

如果是隐式(implicit )intent,android默认给加上一个CATEGORY_DEFAULT,这样的话如果intent filter中没有android.intent.category.DEFAULT这个category的话,匹配测试就会失败。所以,如果你的 activity支持接收implicit intent的话就一定要在intent filter中android.intent.

UITextField+Category

import “UITextField+Category.h” @implementation UITextField (Category) - (void)setTextFieldInputAccessoryView{ UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 30)]; [

iOS中为category添加@property支持

Objective-C runtime提供了Associative References支持,也就是说每一个对象都有一个可选的dictionary字典,我们可以向其添加key/value对。 这是一个非常强大的功能,我们都知道Objective-C中支持category,我们可以在category中添加方法,但是它不允许我们添加实例变量。 通过这个Associati

分类器对未见过类别(unseen category)的识别问题

这篇文章比较旧了,其实是讨论开集识别问题的(Open-Set Problem),可以参考本人的新文章:真实世界中的开集识别问题(Open-Set Recognition Problem) 未见过类别     未见过类别(unseen category)或者叫不知道类别(unknown category)。简单来说,如我们定义类别{苹果,香蕉} 且其数据集为D,那么定义一个二分类器C,将D分割为

LTE category New

Category终端能力等级,表明了终端所支持的数据处理能力(下载、上传速率)、最大的空分复用、调制编码能力等。   详细可参考 3GPP36.306。在R8版本中定义了5类终端,在R10中定义了8类终端....随着版本演进,不断有新的终端被定义!  R8 R9 R10 R11 R12 Category 1 Category 1 Category 1