zoukankan      html  css  js  c++  java
  • fileManager move/remove/create/copy

     // 查看文件夹下的文件

        NSFileManager *fm = [NSFileManager defaultManager];

        NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

        

        NSArray *fmarr = [fm contentsOfDirectoryAtPath:filePath error:nil];

        [fmarr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

            NSLog(@"there are files at path: %@",obj);

        }];

        

        // 在指定路径创建文件夹

        NSFileManager *fm1 = [NSFileManager defaultManager];

        NSString *filePath1 = @"/Users/tangweiwei/Desktop";

        

        NSString *createPath = [NSString stringWithFormat:@"%@/image",filePath1];

        NSString *createpath1 = [NSString stringWithFormat:@"%@/MessageQueueImage",filePath1];

        

        if (![[NSFileManager defaultManager]fileExistsAtPath:createPath]) {

            [fm1 createDirectoryAtPath:createPath withIntermediateDirectories:YES attributes:nil error:nil];

            [fm1 createDirectoryAtPath:createpath1 withIntermediateDirectories:YES attributes:nil error:nil];

        }else {

            NSLog(@"create file failed");

        }

        

        // 删除文件夹

        NSFileManager *deleteObj = [NSFileManager defaultManager];

        NSString *deletePath = [NSString stringWithFormat:@"/Users/tangweiwei/Desktop"];

        NSString *dpath = [deletePath stringByAppendingPathComponent:@"image"];

        NSString *dpath2 = [deletePath stringByAppendingString:@"/MessageQueueImage"];

        BOOL dResult = [deleteObj removeItemAtPath:dpath error:nil];

        BOOL dResult2 = [deleteObj removeItemAtPath:dpath2 error:nil];

        if (dResult) {

            NSLog(@"remove file success");

        }

        if (dResult2) {

            NSLog(@"remove file2 success");

        }

        

        // 移动文件夹

        NSFileManager *newFM = [NSFileManager defaultManager];

        NSString *newFile = NSTemporaryDirectory();

        NSString *fileName = [newFile stringByAppendingPathComponent:@"image"];

        [newFM createDirectoryAtPath:fileName withIntermediateDirectories:YES attributes:nil error:nil];

        

        

        NSString *destination = [NSString stringWithFormat:@"/Users/tangweiwei/Desktop/未命名文件夹"];

     

        NSString *originPath = fileName;

        

        NSString *newString = [destination stringByAppendingPathComponent:@"copied"];

        BOOL copyResult = [newFM copyItemAtPath:originPath toPath:newString error:nil];

        if (copyResult) {

            NSLog(@"copied");

        }

        

        // 移动文件

        NSString *destinateFile = [destination stringByAppendingPathComponent:@"moveFile"];

        BOOL moveResult = [newFM moveItemAtPath:originPath toPath:destinateFile error:nil];

        if (moveResult) {

            NSLog(@"moved");

        }

     

     

    //     在指定文件夹创建图片

        UIImage *img = [UIImage imageNamed:@"123"];

        NSData *data = UIImagePNGRepresentation(img);

     

        NSData *data1 = [@"123321" dataUsingEncoding:NSUTF8StringEncoding];

        NSLog(@"data  :%@,data1:%@",data,data1);

        

        NSFileManager *fm11 = [NSFileManager defaultManager];

        NSString *filePath11 = [@"/Users/tangweiwei/Desktop/未命名文件夹" stringByAppendingPathComponent:@"newImage.png"];

        NSString *newPath = filePath11;

        BOOL result = [fm11 createFileAtPath:newPath contents:data attributes:nil];

        if (result) {

            NSLog(@"success to creat file");

        }

  • 相关阅读:
    Spring Boot四:配置文件详解properties
    程序员常去的14个顶级开发社区
    Spring Boot二:创建第一个web工程 hello world
    Java架构工程师知识图
    Java高效开发12个精品库
    华为员工:表面光鲜 工作十年买不起房
    如果你是一个程序员,又要踏足人工智能领域,应该要知道哪些语言
    H5表单提交上传图片
    转:正则表达式
    转载:jsp九大内置对象和四大作用域
  • 原文地址:https://www.cnblogs.com/tony0571/p/5450652.html
Copyright © 2011-2022 走看看