文件管理器 在沙盒中移动 复制 删除 文件

本文深入探讨了iOS开发中文件路径操作、文件复制、移动、创建文件夹及删除等核心功能,通过实例展示了如何使用NSFileManager进行文件管理,包括复制、移动文件,创建和删除文件夹,并详细介绍了获取文件路径、判断文件存在性等实用技巧。

//单例类

    #pragma mark - 创建对象

    NSFileManager *fileManager = [NSFileManager defaultManager];

    

    //文件路径

    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"25-1.plist"];

    

    //判断一个文件是否存在 传参路径

    BOOL isExist = [fileManager fileExistsAtPath:filePath];

    if (isExist)

    {

        NSLog(@"文件存在");

    }

    

    #pragma mark - 复制 源路径 目的路径

    //程序安装后 沙盒中.app的绝对路径

    //NSLog(@"bundle=====%@", [[NSBundle mainBundle] bundlePath]);

    //文件的绝对路径

    

    NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"25-1" ofType:@"plist"];

    

    //NSLog(@"resourcePath===%@", resourcePath);

    //访问另外一个bundle

    

    NSBundle *anotherBundle = [NSBundle bundleWithPath:@"~/NSFileManager 文件管理器.app/mapapi.bundle/images"];

    //打印bundle的路径

    NSLog(@"anotherBundlePath === %@", [anotherBundle bundlePath]);

    

    //获取该目录下的所有文件或文件夹 传参为哪个目录

    NSArray *directories = [fileManager contentsOfDirectoryAtPath:[anotherBundle bundlePath] error:nil];

    NSLog(@"directories====%@", directories);

    //相对路径

    //UIImage *image = [UIImage imageNamed:@""];

    

    //目的路径

    NSString *destinationPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"25-1.plist"];


    //如果文件不存在 复制操作

    if (![fileManager fileExistsAtPath:destinationPath])

    {

        //复制操作

        BOOL isCopy = [fileManager copyItemAtPath:resourcePath toPath:destinationPath error:nil];

        if (isCopy)

            NSLog(@"复制成功");

    }

    

    #pragma mark - 移动  新建文件夹  源路径  目的路径

    //移动目的路径

    NSString *moveDestinationPath = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"25-1.plist"];

    BOOL isMove = [fileManager moveItemAtPath:destinationPath toPath:moveDestinationPath error:nil];

    if (isMove) NSLog(@"移动OK");

    

    //创建文件夹路径

    NSString *creatDirectoryPath = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"a1/a2/a3"];

    NSLog(@"creatDirectoryPath===%@", creatDirectoryPath);

    

    //是否创建文件夹成功

    NSLog(@"%@", [fileManager createDirectoryAtPath:creatDirectoryPath withIntermediateDirectories:NO attributes:nil error:nil]?@"创建文件夹成功":@"创建文件夹失败");

    

    //文件新路径

    NSString *newFilePath = [creatDirectoryPath stringByAppendingPathComponent:@"25-1.plist"];

    

    //移动失败 因为创建文件夹时createIntermediates该参数为NO 获得不到路径

    NSLog(@"%@", [fileManager moveItemAtPath:moveDestinationPath toPath:newFilePath error:nil]?@"移动成功":@"移动失败");

    //创建文件夹

    /*

     第一个参数 创建文件夹路径

     第二个参数 创建目的文件夹时 是否创建中间不存在的文件夹

     */

    [fileManager createDirectoryAtPath:creatDirectoryPath withIntermediateDirectories:YES attributes:nil error:nil];

    

    //再次创建文件夹  createIntermediatesYES 创建成功

    NSLog(@"%@", [fileManager moveItemAtPath:moveDestinationPath toPath:newFilePath error:nil]?@"第二次移动成功":@"第二次移动失败");

    

    //删除

    BOOL isRemove = [fileManager removeItemAtPath:newFilePath

                      error:nil];

    if (isRemove) {

        NSLog(@"oh a waste of time");

    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值