• 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");

        }

  • 相关阅读:
    [LeetCode] 875. Koko Eating Bananas 科科吃香蕉
    [LeetCode] 874. Walking Robot Simulation 走路机器人仿真
    [LeetCode] 995. Minimum Number of K Consecutive Bit Flips 连续K位翻转的最小次数
    [LeetCode] 873. Length of Longest Fibonacci Subsequence 最长的斐波那契序列长度
    [LeetCode] 872. Leaf-Similar Trees 叶结点相似的树
    [LeetCode] 870. Advantage Shuffle 优势洗牌
    [LeetCode] 869. Reordered Power of 2 重新排序为2的倍数
    [LeetCode] 868. Binary Gap 二进制间隙
    [LeetCode] 867. Transpose Matrix 转置矩阵
    [LeetCode] 866. Prime Palindrome 质数回文数
  • 原文地址:https://www.cnblogs.com/tony0571/p/5450652.html
Copyright © 2020-2023  润新知