OC循环渐进:文件管理常用操作

介绍以下文件的常规用法:
以下为一个代码示例:
ViewController.m的代码文件如下:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

//---------------------路径操作----------------------------------------
    //获得沙盒路径
    NSString *root = NSHomeDirectory();
    NSLog(@"root = %@",root);

    //获得Documents目录路径
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSLog(@"documentPath = %@",documentsPath);

    //获得Library目录的路径
    NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)  firstObject];
    NSLog(@"libraryPath = %@",libraryPath);

    //获得tmp目录的路径
    NSString *tmpPath = NSTemporaryDirectory();
    NSLog(@"tmpPath = %@",tmpPath);

    //获得app目录的路径
    NSString *appPath = [[NSBundle mainBundle] bundlePath];
    NSLog(@"appPath = %@",appPath);



//-------------------文件操作---------------------------------------------

//1.在tmp文件夹下创建一个目录tempDir,并写入文件

    NSError *error;

    NSFileManager *fm = [NSFileManager defaultManager];
    NSString *tempFile = [tmpPath stringByAppendingPathComponent:@"temDir"];
    if (![fm fileExistsAtPath:tempFile]) {
        if ([fm createDirectoryAtPath:tempFile withIntermediateDirectories:YES attributes:nil error:&error]) {
            NSLog(@"目录创建成功!");
        }else {
            NSLog(@"error = %@",error.debugDescription);
        }
    }else {
        NSLog(@"目录已经存在!!!");
    }


//2.使用系统的WriteToFile创建文件:

    if ([fm fileExistsAtPath:tempFile]) {

        //NSString
        NSString *content = @"This is String !!!";
        NSString *stringPath = [tempFile stringByAppendingPathComponent:@"string.txt"];
        if (![fm fileExistsAtPath:stringPath]) {
            [content writeToFile:stringPath atomically:YES encoding:NSUTF8StringEncoding error:&error];
            NSLog(@"文本文件写入成功!");
        }else {
            NSLog(@"文件已经存在!");
        }

        //NSArray
        NSArray *arrayContent = [NSArray arrayWithObjects:@"张三",@"李四",nil];
        NSString *arrayPath = [tempFile stringByAppendingPathComponent:@"array.plist"];
        if (![fm fileExistsAtPath:arrayPath]) {
            [arrayContent writeToFile:arrayPath atomically:YES];
            NSLog(@"数组文件写入成功!");
        }else {
            NSLog(@"文件已经存在!");
        }

        //NSDictionary
        NSDictionary *dictContent = @{@"name":@"张三",@"age":@15};
        NSString *dictPath = [tempFile stringByAppendingPathComponent:@"dcit.plist"];
        if (![fm fileExistsAtPath:dictPath]) {
            [dictContent writeToFile:dictPath atomically:YES];
            NSLog(@"字典文件写入成功!");
        }else {
            NSLog(@"文件已经存在!");
        }


    }

//使用NSFileManager文件管理类下载图片
    NSString *pic1 = @"http://img05.tooopen.com/images/20150804/tooopen_sl_136730147132.jpg";
    NSString *pic2 = @"http://img5.imgtn.bdimg.com/it/u=18143384611893904322&fm=23&gp=0.jpg";
    NSString *pic3 = @"http://img02.tooopen.com/images/20150807/tooopen_sl_137007777362.jpg";
    NSString *pic4 = @"http://img02.tooopen.com/images/20150806/tooopen_sl_136886487563.jpg";
    NSString *pic5 = @"http://img05.tooopen.com/images/20140917/sl_95734016877.jpg";
    NSArray *imgsArray = @[pic1,pic2,pic3,pic4,pic5];
    NSString *imgsFile = [tempFile stringByAppendingPathComponent:@"images"];
    BOOL isSuccess = [fm createDirectoryAtPath:imgsFile withIntermediateDirectories:YES attributes:nil error:nil];
    if (isSuccess) {
        NSLog(@"images文件夹创建成功!");
    }else {
         NSLog(@"images文件夹创建失败!");
        return;
    }

    for (int i = 0; i < imgsArray.count; i++) {
        NSString *imgPath = [imgsArray[i] lastPathComponent];
        imgPath = [imgsFile stringByAppendingPathComponent:imgPath];
        if (![fm fileExistsAtPath:imgPath]) {
            NSString *urlPath = [imgsArray[i] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlPath]];
            if (data == nil) {
                NSLog(@"网络连接错误!!!");
            }else {
                if ([fm createFileAtPath:imgPath contents:data attributes:nil]) {
                     NSLog(@"图片下载成功!");
                }else {
                    NSLog(@"文件下载失败!");
                }
            }

        }else {
            NSLog(@"图片已经存在!");
        }
    }

//读取images文件夹的大小(两种方式)

    NSArray *imgsFileArray = [fm subpathsAtPath:imgsFile];
    CGFloat len;
    for (NSString *path in imgsFileArray) {
        NSData *data = [NSData dataWithContentsOfFile:[imgsFile stringByAppendingPathComponent:path]];
        len += data.length;
    }
    len = len / 1024/ 1024;

    NSLog(@"文件的%.2fM",len);

    CGFloat size;
    for (NSString *path in imgsFileArray) {
        NSDictionary *dict = [fm attributesOfItemAtPath:[imgsFile stringByAppendingPathComponent:path] error:nil];
       size += [[dict objectForKey:NSFileSize]  floatValue];
    }
    size = size  / 1024 / 1024 ;

    NSLog(@"文件大小为%.2fM",size);




//删除images文件

    for (NSString *path in imgsFileArray) {
        NSString *delPath  = [imgsFile stringByAppendingPathComponent:path];
        [fm removeItemAtPath:delPath error:nil];
        NSLog(@"删除图片成功!!!");
    }



}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

测试结果如下:

2015-08-12 18:56:41.102 文件管理02[25358:879307] root = /Users/3017/Library/Developer/CoreSimulator/Devices/812D2045-63E4-41EC-954A-DB33074077B0/data/Containers/Data/Application/3373FDE5-9D40-413A-A44D-2D4BB555A5F8
2015-08-12 18:56:41.103 文件管理02[25358:879307] documentPath = /Users/3017/Library/Developer/CoreSimulator/Devices/812D2045-63E4-41EC-954A-DB33074077B0/data/Containers/Data/Application/3373FDE5-9D40-413A-A44D-2D4BB555A5F8/Documents
2015-08-12 18:56:41.104 文件管理02[25358:879307] libraryPath = /Users/3017/Library/Developer/CoreSimulator/Devices/812D2045-63E4-41EC-954A-DB33074077B0/data/Containers/Data/Application/3373FDE5-9D40-413A-A44D-2D4BB555A5F8/Library
2015-08-12 18:56:41.104 文件管理02[25358:879307] tmpPath = /Users/3017/Library/Developer/CoreSimulator/Devices/812D2045-63E4-41EC-954A-DB33074077B0/data/Containers/Data/Application/3373FDE5-9D40-413A-A44D-2D4BB555A5F8/tmp/
2015-08-12 18:56:41.104 文件管理02[25358:879307] appPath = /Users/3017/Library/Developer/CoreSimulator/Devices/812D2045-63E4-41EC-954A-DB33074077B0/data/Containers/Bundle/Application/D9F157BB-8F34-47A5-A2B9-2804E668AAD5/文件管理02.app
2015-08-12 18:56:41.105 文件管理02[25358:879307] 目录创建成功!
2015-08-12 18:56:41.127 文件管理02[25358:879307] 文本文件写入成功!
2015-08-12 18:56:41.134 文件管理02[25358:879307] 数组文件写入成功!
2015-08-12 18:56:41.136 文件管理02[25358:879307] 字典文件写入成功!
2015-08-12 18:56:41.136 文件管理02[25358:879307] images文件夹创建成功!
2015-08-12 18:56:42.135 文件管理02[25358:879307] 图片下载成功!
2015-08-12 18:56:42.496 文件管理02[25358:879307] 网络连接错误!!!
2015-08-12 18:56:43.881 文件管理02[25358:879307] 图片下载成功!
2015-08-12 18:56:44.172 文件管理02[25358:879307] 图片下载成功!
2015-08-12 18:56:44.530 文件管理02[25358:879307] 图片下载成功!
2015-08-12 18:56:44.532 文件管理02[25358:879307] 文件的0.04M
2015-08-12 18:56:44.532 文件管理02[25358:879307] 文件大小为0.04M
2015-08-12 18:56:44.533 文件管理02[25358:879307] 删除图片成功!!!
2015-08-12 18:56:44.534 文件管理02[25358:879307] 删除图片成功!!!
2015-08-12 18:56:44.534 文件管理02[25358:879307] 删除图片成功!!!
2015-08-12 18:56:44.535 文件管理02[25358:879307] 删除图片成功!!!

tmp目录截图如下:
全屏图:
这里写图片描述
页面图:
这里写图片描述
当然,上面仅仅是一些常用的方法,更多内容请参考
文件操作(NSFileManager)iOS学习之iOS沙盒(sandbox)机制和文件操作之NSFileManager

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值