码迷,mamicode.com
首页 > 移动开发 > 详细

iOS文件的管理(添加,删除,拷贝,移动)

时间:2016-04-29 23:33:35      阅读:421      评论:0      收藏:0      [点我收藏+]

标签:

#import "ViewController.h"

 

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    [self handleNSFileManage];

}

 

// 文件管理

- (void)handleNSFileManage{

    // NSFileManager 是一个单例类,我们称之为文件管理类,是一个专门用来管理文件的工具,主要可以完成以下功能:文件的添加,文件的删除,文件的移动,文件的拷贝;

    // 创建文件管理对象

    NSFileManager *fileManage = [NSFileManager defaultManager];

 

    // 1.文件的添加

    // 例如:要在Documents文件夹下创建一个File1文件夹

    // ①首先要获取Documents文件夹路径

    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES)lastObject];

    

    // ②接着需要准备要创建的文件路径

    NSString *File1Path = [documentsPath stringByAppendingPathComponent:@"Flie1"];

     

    // ③创建文件夹

    // 参数1:文件路径

    // 参数2:如果文件中已经有别的目录,是否还要创建

    // 参数3,4:属性,报错信息,都给nil

    // 用来判断要创建的文件是否存在

    BOOL isHave = [fileManage fileExistsAtPath:File1Path];

    if (isHave) {

        NSLog(@"文件已存在");

    }else{

        NSLog(@"文件不存在");

       BOOL isSuccess = [fileManage createDirectoryAtPath:File1Path withIntermediateDirectories:YES attributes:nil error:nil];

        NSLog(@"%@",isSuccess ? @"创建成功" : @"创建失败");

    }

    NSLog(@"%@",File1Path);

    

    

    // 2.文件的删除

    // 判断要删除的文件是否存在

    if ([fileManage fileExistsAtPath:File1Path]) {

        NSLog(@"文件存在");

        // 删除

        BOOL isSuccess = [fileManage removeItemAtPath:File1Path error:nil];

        NSLog(@"%@",isSuccess ? @"删除成功" : @"删除失败");

    }else{

        NSLog(@"文件不存在");

    }

 

    // 3.文件的拷贝

    //  简单示范:准备一个Love.txt文件拖入工程,拷贝到File1文件夹中  

    // ①获取要拷贝的文件路径

    NSString *lovePath = [[NSBundle mainBundle]pathForResource:@"Love" ofType:txt];

    

    // ②准备要拷贝过去的文件路径

    NSString *toLovePath = [File1Path stringByAppendingPathComponent:@"Love.txt"];

    // 简单判断拷贝过去的文件路径是否存在

    if (![fileManage fileExistsAtPath:toLovePath]) {

       BOOL isSuccess = [fileManage copyItemAtPath:lovePath toPath:toLovePath error:nil];

        NSLog(@"%@",isSuccess ? @"拷贝成功" : @"拷贝失败");

    }

    

    // 4.文件的移动

    // 例如:将file1文件移动到library文件夹下

    // ①获取library文件路径

    NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)lastObject];

    

    // 获取在libraryPath文件中加入file1的路径

    NSString *toFilePath = [libraryPath stringByAppendingPathComponent:@"File1"];

    if (![fileManage fileExistsAtPath:toFilePath]) {

        BOOL isSuccess = [fileManage moveItemAtPath:File1Path toPath:toFilePath error:nil];

        NSLog(@"%@",isSuccess ? @"移动成功" : @"移动失败");

    }

   

    // 通过NSFileManage计算文件的大小

    // 计算toLovePath路径下的文件大小

    // NSDictionary *info = [fileManage attributesOfItemAtPath:toLovePath error:nil];

    // NSLog(@"%lfM",info.fileSize/1024.0/1024.0);

    

}

 

iOS文件的管理(添加,删除,拷贝,移动)

标签:

原文地址:http://www.cnblogs.com/Mr-zyh/p/5447736.html

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