iOS文件夾操作

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. 創(chuàng)建文件夾

.h

/**
 創(chuàng)建文件夾

 @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.移動(dòng)文件夾

.h

/**
 移動(dòng)文件夾

 @param filePath 文件路徑
 @param toPath 移動(dòng)路徑
 */
- (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.移動(dòng)文件

.h

/**
 移動(dòng)文件

 @param fileName 文件名
 @param filePath 文件路徑
 @param toPath 移動(dòng)路徑
 */
- (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];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容