文件夹操作

作者: 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];
    }
    

    相关文章

      网友评论

        本文标题:文件夹操作

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