码迷,mamicode.com
首页 > 其他好文 > 详细

fileManager move/remove/create/copy

时间:2016-05-01 17:38:06      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

 // 查看文件夹下的文件

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

    }

fileManager move/remove/create/copy

标签:

原文地址:http://www.cnblogs.com/tony0571/p/5450652.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!