博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
fileManager move/remove/create/copy
阅读量:4480 次
发布时间:2019-06-08

本文共 2885 字,大约阅读时间需要 9 分钟。

 // 查看文件夹下的文件

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

    }

转载于:https://www.cnblogs.com/tony0571/p/5450652.html

你可能感兴趣的文章
jsp中${}是EL表达式的常规表示方式
查看>>
GoldenGate常见问题及处理
查看>>
Android JNI学习(五)——Demo演示
查看>>
SSRS 呈现Barcode Free
查看>>
java快速排序引起的StackOverflowError异常
查看>>
泛函编程(35)-泛函Stream IO:IO处理过程-IO Process
查看>>
-XX:-PrintClassHistogram 按下Ctrl+Break后,打印类的信息
查看>>
mac 安装php redis扩展
查看>>
css3中Animation
查看>>
JS 判断是否是手机端并跳转操作
查看>>
最短路径问题(dijkstra-模板)
查看>>
c# 导出表格 api
查看>>
使用Android NDK以及JNI编写应用
查看>>
学习笔记之-php数组数据结构
查看>>
初学者--bootstrap(六)组件中的下拉菜单----在路上(10)
查看>>
QMetaObject::connectSlotsByName 总结
查看>>
Android 微信支付步骤
查看>>
js操作table
查看>>
JQuery学习系列篇(一)
查看>>
Centos7 minimal 系列之rabbitmq安装(八)
查看>>