zoukankan      html  css  js  c++  java
  • iOS文件的管理(添加,删除,拷贝,移动)

    #import "ViewController.h"

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        [self handleNSFileManage];

    }

    // 文件管理

    - (void)handleNSFileManage{

        // NSFileManager 是一个单例类,我们称之为文件管理类,是一个专门用来管理文件的工具,主要可以完成以下功能:文件的添加,文件的删除,文件的移动,文件的拷贝;

        // 创建文件管理对象

        NSFileManager *fileManage = [NSFileManager defaultManager];

        // 1.文件的添加

        // 例如:要在Documents文件夹下创建一个File1文件夹

        // ①首先要获取Documents文件夹路径

        NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES)lastObject];

        

        // ②接着需要准备要创建的文件路径

        NSString *File1Path = [documentsPath stringByAppendingPathComponent:@"Flie1"];

         

        // ③创建文件夹

        // 参数1:文件路径

        // 参数2:如果文件中已经有别的目录,是否还要创建

        // 参数3,4:属性,报错信息,都给nil

        // 用来判断要创建的文件是否存在

        BOOL isHave = [fileManage fileExistsAtPath:File1Path];

        if (isHave) {

            NSLog(@"文件已存在");

        }else{

            NSLog(@"文件不存在");

           BOOL isSuccess = [fileManage createDirectoryAtPath:File1Path withIntermediateDirectories:YES attributes:nil error:nil];

            NSLog(@"%@",isSuccess ? @"创建成功" : @"创建失败");

        }

        NSLog(@"%@",File1Path);

        

        

        // 2.文件的删除

        // 判断要删除的文件是否存在

        if ([fileManage fileExistsAtPath:File1Path]) {

            NSLog(@"文件存在");

            // 删除

            BOOL isSuccess = [fileManage removeItemAtPath:File1Path error:nil];

            NSLog(@"%@",isSuccess ? @"删除成功" : @"删除失败");

        }else{

            NSLog(@"文件不存在");

        }

        // 3.文件的拷贝

        //  简单示范:准备一个Love.txt文件拖入工程,拷贝到File1文件夹中  

        // ①获取要拷贝的文件路径

        NSString *lovePath = [[NSBundle mainBundle]pathForResource:@"Love" ofType:txt];

        

        // ②准备要拷贝过去的文件路径

        NSString *toLovePath = [File1Path stringByAppendingPathComponent:@"Love.txt"];

        // 简单判断拷贝过去的文件路径是否存在

        if (![fileManage fileExistsAtPath:toLovePath]) {

           BOOL isSuccess = [fileManage copyItemAtPath:lovePath toPath:toLovePath error:nil];

            NSLog(@"%@",isSuccess ? @"拷贝成功" : @"拷贝失败");

        }

        

        // 4.文件的移动

        // 例如:将file1文件移动到library文件夹下

        // ①获取library文件路径

        NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)lastObject];

        

        // 获取在libraryPath文件中加入file1的路径

        NSString *toFilePath = [libraryPath stringByAppendingPathComponent:@"File1"];

        if (![fileManage fileExistsAtPath:toFilePath]) {

            BOOL isSuccess = [fileManage moveItemAtPath:File1Path toPath:toFilePath error:nil];

            NSLog(@"%@",isSuccess ? @"移动成功" : @"移动失败");

        }

       

        // 通过NSFileManage计算文件的大小

        // 计算toLovePath路径下的文件大小

        // NSDictionary *info = [fileManage attributesOfItemAtPath:toLovePath error:nil];

        // NSLog(@"%lfM",info.fileSize/1024.0/1024.0);

        

    }

  • 相关阅读:
    Vue的diff算法是如何操作运用的?本文教你
    最新vue-router的hooks用法你会吗?本文详解
    vue 3.x 如何高效学成?本文详解
    如何用webpack搭建vue项目?本文案例详解
    TypeScript考试题来了,60%的人不会(附答案)
    动手动脑之查看String.equals()方法的实现代码及解释
    整理string类常见方法的使用说明
    古罗马皇帝的子串加密
    大道至简第四章阅读感想
    大道至简第三章阅读感想
  • 原文地址:https://www.cnblogs.com/Mr-zyh/p/5447736.html
Copyright © 2011-2022 走看看