NSFileManager文件操作

// 获取Documents路径

- (NSString *)getDocumentPath{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) firstObject];
    return  path;
}

// 创建文件夹

- (void)createDirectory{
    NSString *documentPath = [self getDocumentPath];
    NSFileManager *filemanager = [NSFileManager defaultManager];
    NSString *iosDirectory = [documentPath stringByAppendingPathComponent:@"ios"];
    BOOL isSuccess = [filemanager createDirectoryAtPath:iosDirectory withIntermediateDirectories:YES attributes:nil error:nil];
    if (isSuccess) {
        NSLog(@"success");
    }else NSLog(@"fail");
    
}

// 创建文件

- (void)createFile{
    NSString *documentsPath = [self getDocumentPath];
    NSFileManager *filemanager = [NSFileManager defaultManager];
    NSString *iospath = [documentsPath stringByAppendingPathComponent:@"ios.txt"];
    BOOL isSuccess = [filemanager createFileAtPath:iospath contents:nil attributes:nil];
    if (isSuccess) {
        NSLog(@"success");
    }else NSLog(@"fail");

}

// 写文件

- (void)writeFile{
    NSString *documentsPath = [self getDocumentPath];
    NSString *iosPath = [documentsPath stringByAppendingPathComponent:@"ios.txt"];
    NSString *content = @"我是一个数据拉";
    BOOL isSuccess = [content writeToFile:iosPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    if (isSuccess) {
        NSLog(@"success");
    }else{
        NSLog(@"fail");
    }
}

// 读取文件内容

- (void)readFileContent{
    NSString *documentsPath = [self getDocumentPath];
    NSString *iosPath = [documentsPath stringByAppendingPathComponent:@"ios.txt"];
    NSString *content = [NSString stringWithContentsOfFile:iosPath encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"read success --%@",content);
    NSUInteger num = [self fileSizeAtPath:iosPath];
    NSLog(@"---%lud",(unsigned long)num);
}

// 判断文件是否存在

- (BOOL)isSxistAtPath:(NSString *)filePath{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isExist = [fileManager fileExistsAtPath:filePath];
    return isExist;
}

// 计算文件大小

- (unsigned long long)fileSizeAtPath:(NSString *)filePath{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isExist = [fileManager fileExistsAtPath:filePath];
    if (isExist) {
        unsigned long long fileSize = [[fileManager attributesOfItemAtPath:filePath error:nil] fileSize];
        return  fileSize;
    }else{
        NSLog(@"file is not exist");
        return 0;
    }
}

// 计算整个文件夹中所有的文件大小

- (unsigned long long)folderSizeAtPath:(NSString *)folderPath{
    NSFileManager *filemanager = [NSFileManager defaultManager];
    BOOL isExsit = [filemanager fileExistsAtPath:folderPath];
    if (isExsit) {
        NSEnumerator *chileFileEnumerator = [[filemanager subpathsAtPath:folderPath] objectEnumerator];
        unsigned long long folderSize = 0;
        NSString *fileName = @"";
        while ((fileName = [chileFileEnumerator nextObject]) != nil) {
            NSString *fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
            folderSize += [self fileSizeAtPath:fileAbsolutePath];
        }
        return folderSize / (1024.0 * 1024.0);
    }else{
        NSLog(@"file is not exist");
        return 0;
    }
}

// 删除文件

- (void)deleteFile{
    NSString *documentPath = [self getDocumentPath];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *iosPath = [documentPath stringByAppendingPathComponent:@"ios.txt"];
    BOOL isSuccess = [fileManager removeItemAtPath:iosPath error:nil];
    if (isSuccess) {
        NSLog(@"delete success");
    }else{
        NSLog(@"delete fail");
    }
}

// 移动文件

- (void)moveFileName{
    NSString *documentsPath =[self getDocumentPath];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"ios.txt"];
    NSString *moveToPath = [documentsPath stringByAppendingPathComponent:@"ios1.txt"];
    BOOL isSuccess = [fileManager moveItemAtPath:filePath toPath:moveToPath error:nil];
    if (isSuccess) {
        NSLog(@"move success");
    }else{
        NSLog(@"move fail");
    }
}

// 重命名

- (void)renameFileName{
    /*
    文件重命名的两个方法:
    方法一、思路:1.提取原有文件data,
    2.将data存入新建的文件,
    3.删除原有文件
    方法二、用[manager moveItemAtPath:filePath toPath:newPath error:nil];方法将filepath
    路径下的文件名换成newPath
    对文件NSData赋值,在文件内将内容更改后,move后的文件内容保持跟最后修改的一致
    如果文件NSdata未赋值,在程序外文件内更改,move后新文件仍为空。
     */
    // 通过移动文件对文件重命名
    NSString *documentsPath = [self getDocumentPath];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"ios.txt"];
    NSString *moveToPath = [documentsPath stringByAppendingPathComponent:@"rename.txt"];
    BOOL isSuccess = [fileManager moveItemAtPath:filePath toPath:moveToPath error:nil];
    if (isSuccess) {
        NSLog(@"rename success");
    }else{
        NSLog(@"rename fail");
    }
}

// 文件复制

- (void)copyFileName{
    NSString *documentsPath = [self getDocumentPath];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"ios.txt"];
    NSString *moveToPath = [documentsPath stringByAppendingPathComponent:@"rename.txt"];
    if ([fileManager copyItemAtPath:filePath toPath:moveToPath error:nil]) {
        NSLog(@"success");
    }else{
        NSLog(@"fail");
    }
}

你可能感兴趣的:(NSFileManager文件操作)