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

        }

  • 相关阅读:
    Windows Server 设置自动登陆
    Kettle学习笔记(四)— 总结
    Kettle学习笔记(一)— 环境部署及运行
    Web项目自动打开并且全屏
    数据库SQL Server 2016“功能选择”详细说明及精简安装选择
    kettle学习笔记(三)— 定时任务的脚本执行
    Kettle学习笔记(二)— 基本操作
    effective C++ 条款 54:让自己熟悉包括TR1在内的标准程序库
    [转]基于MFC的ActiveX控件开发
    effective C++ 条款 50:了解new和delete的合理替换时机
  • 原文地址:https://www.cnblogs.com/tony0571/p/5450652.html
Copyright © 2011-2022 走看看