文件夹操作

作者: YvanLiu | 来源:发表于2019-05-14 08:58 被阅读1次

1.单例

.h

@interface PackageManager : NSObject

@property (strong, nonatomic) NSString * documentPath;
@property (strong, nonatomic) NSString * cachesPath;

+ (instancetype)shareManager;

.m

static PackageManager * manager;

@implementation PackageManager

+ (instancetype)shareManager {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[self alloc]init];
    });
    return manager;
}

- (instancetype)init {
    if (self = [super init]) {
        self.cachesPath   = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
        self.documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    }
    return self;
}

2. 创建文件夹

.h

/**
 创建文件夹

 @param filePath 文件夹路径
 */
- (void)createFolderWithFile:(NSString *)filePath;

.m

- (void)createFolderWithFile:(NSString *)filePath {
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDir;
    BOOL isExit = [fileManager fileExistsAtPath:filePath isDirectory:&isDir];
    
    if (!isExit || !isDir)
    {
        [fileManager createDirectoryAtPath:filePath
               withIntermediateDirectories:YES
                                attributes:nil
                                     error:nil];
    }
}

3.删除文件夹

.h

/**
 删除文件夹

 @param filePath 文件夹路径
 */
- (void)removeFolderWithFile:(NSString *)filePath;

.m


- (void)removeFolderWithFile:(NSString *)filePath {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath])
    {
        [fileManager removeItemAtPath:filePath error:nil];
    }
}

3.拷贝文件夹

.h

/**
 拷贝文件夹

 @param filePath 文件路径
 @param toPath 拷贝路径
 */
- (void)copyFlolderFrom:(NSString *)filePath to:(NSString *)toPath;

.m

- (void)copyFlolderFrom:(NSString *)filePath to:(NSString *)toPath {
    NSError *error;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    if ([fileManager fileExistsAtPath:toPath])
    {
        [fileManager removeItemAtPath:toPath error:&error];
    }
    [fileManager copyItemAtPath:filePath toPath:toPath error:&error];
}

4.拷贝文件

.h

/**
 拷贝文件

 @param fileName 文件名称
 @param filePath 文件路径
 @param toPath 拷贝路径
 */
- (void)copyFillWithFile:(NSString *)fileName from:(NSString *)filePath to:(NSString *)toPath;

.m


- (void)copyFillWithFile:(NSString *)fileName from:(NSString *)filePath to:(NSString *)toPath {
    NSError *error;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString * fileToPath = [toPath stringByAppendingPathComponent:fileName];
    
    if ([fileManager fileExistsAtPath:fileToPath])
    {
        [fileManager removeItemAtPath:fileToPath error:&error];
    }
    [fileManager copyItemAtPath:[filePath stringByAppendingPathComponent:fileName]
                         toPath:toPath
                          error:&error];
}

5.移动文件夹

.h

/**
 移动文件夹

 @param filePath 文件路径
 @param toPath 移动路径
 */
- (void)moveFolderFrom:(NSString *)filePath to:(NSString *)toPath;

.m

- (void)moveFolderFrom:(NSString *)filePath to:(NSString *)toPath {
    NSError *error;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:toPath])
    {
        [fileManager removeItemAtPath:toPath error:&error];
    }
    [fileManager moveItemAtPath:filePath toPath:toPath error:&error];
}

6.移动文件

.h

/**
 移动文件

 @param fileName 文件名
 @param filePath 文件路径
 @param toPath 移动路径
 */
- (void)moveFillWithFill:(NSString *)fileName from:(NSString *)filePath to:(NSString *)toPath;

.m

- (void)moveFillWithFill:(NSString *)fileName from:(NSString *)filePath to:(NSString *)toPath {
    
    NSError *error;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString * fileToPath = [toPath stringByAppendingPathComponent:fileName];
    
    if ([fileManager fileExistsAtPath:fileToPath])
    {
        [fileManager removeItemAtPath:fileToPath error:&error];
    }
    [fileManager moveItemAtPath:[filePath stringByAppendingPathComponent:fileName]
                         toPath:toPath
                          error:&error];
}

相关文章

  • nodejs 文件操作

    定义文件系统: var fs = require('fs') 一、文件夹操作 文件系统:文件夹和文件操作 文件夹操...

  • C# 文件相关操作

    一、文件夹 1.选择文件夹 2.文件夹相关操作 C#文件夹相关操作主要使用Directory类和Directory...

  • Linux 常用命令

    日常操作命令 文件系统操作 查看目录信息 切换目录 创建文件夹 删除文件夹 修改文件夹名称 创建文件 利用 vi ...

  • 文件夹操作

    //文件夹操作(目录操作) DirectoryInfo info = new DirectoryInf...

  • IDEA找不到配置文件cannot resolve file a

    IDEA没有自动识别resouces文件夹,然后通过网上找到设置文件夹属性的操作,操作是右键resources文件...

  • UE C++操作外部文件整理

    用到的头文件: 文件夹 创建文件夹: 删除文件夹: 打开文件夹: 遍历文件夹: 文件操作 读取文件: 查找文件: ...

  • python中的文件操作-新建与删除

    常见的文件操作 新建文件夹,新建文件,删除文件夹,删除文件 完成以上操作需要引入以下标准库 新建文件夹 新建文件 ...

  • 文件夹操作

    1.创建文件夹 Dir.new %%1 Dir::mkdir #不指定目录全名称时,缺省为工作目录 Dir::ch...

  • 文件夹操作

    1.基础类方法:判断是否有此文件(fileName) 2.基础类方法:根据文件名(fileName)找到文件路径

  • 文件夹操作

    1.单例 .h .m 2. 创建文件夹 .h .m 3.删除文件夹 .h .m 3.拷贝文件夹 .h .m 4.拷...

网友评论

    本文标题:文件夹操作

    本文链接:https://www.haomeiwen.com/subject/hmvraqtx.html