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

来源:互联网 发布:西装面料品牌 知乎 编辑:程序博客网 时间:2024/04/19 23:49

//单例类

    #pragma mark - 创建对象

    NSFileManager *fileManager = [NSFileManagerdefaultManager];

    

    //文件路径

    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 = [[NSBundlemainBundle] pathForResource:@"25-1"ofType:@"plist"];

    

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

    //访问另外一个bundle

    

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

    //打印bundle的路径

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

    

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

   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:destinationPatherror:nil];

       if (isCopy)

           NSLog(@"复制成功");

    }

    

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

    //移动目的路径

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

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

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

    

    //创建文件夹路径

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

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

    

    //是否创建文件夹成功

    NSLog(@"%@", [fileManagercreateDirectoryAtPath:creatDirectoryPath withIntermediateDirectories:NOattributes:nilerror:nil]?@"创建文件夹成功":@"创建文件夹失败");

    

    //文件新路径

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

    

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

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

    //创建文件夹

   /*

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

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

     */

    [fileManager createDirectoryAtPath:creatDirectoryPathwithIntermediateDirectories:YESattributes:nilerror:nil];

    

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

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

    

   //删除

   BOOL isRemove = [fileManager removeItemAtPath:newFilePath

                     error:nil];

   if (isRemove) {

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

    }


0 0
原创粉丝点击