2015ztlucky

导航

文件管理

#import "ViewController.h"

 

@interface ViewController ()

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

/*

 文件管理:

 

 是对沙盒路径下的文件及文件夹进行创建 移动 复制并持久化保存的过程

 文件管理是通过文件管家完成的

 

 沙盒路径:

 

  每个应用程序只对应一个沙盒路径

 当前的应用程序只能访问当前自身的沙盒路径的文件 ,不能访问外界的沙盒路径下的文件,自身的沙盒路径下的文件也不能被其他的应用程序访问

 沙盒路径下的三个文件夹:

 Documents  : 存放持久化数据 例如:(从网络上下载的数据)

 Library: 存放系统数据 和系统缓存 Cache

 temp: 存放临时文件

 

 */

 

//  1.获取沙盒路径

    NSString *homePath = NSHomeDirectory();

//    2.打印沙盒路径

    NSLog(@"homePath = %@",homePath);

    

//   2.拼接Documents 路径

//    2.1 方法一:

//    NSString *documentsPath = [homePath stringByAppendingFormat:@"/Documents"];

//    2.2方法二:

    NSString *documentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

    

//    =====文件管理====

//    1.创建新的文件

//    1.1创建文件管家对象(单例)

    NSFileManager *manager = [NSFileManager defaultManager];

//    1.2 在documents文件夹下创建新的文件

//    1.2.1 创建存放的文件的内容

    NSString *str = @"gfhhfhh";

    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];

    BOOL isOk = [manager createFileAtPath:[documentsPath  stringByAppendingString:@"/test.txt"] contents:data attributes:nil];

    if (isOk) {

        NSLog(@"文件创建成功");

        

    }

    else{

        NSLog(@"文件创建失败");

    }

//    2.创建文件夹

    NSError *error = nil;

    BOOL ok =[manager createDirectoryAtPath:[documentsPath stringByAppendingPathComponent:@"test2"] withIntermediateDirectories:YES attributes:nil error:&error];

    

    if (ok) {

        NSLog(@"文件夹创建成功");

    }else{

        NSLog(@"error is : %@",error);

    }

    

//    3.读取文件的内容

//    3.1方式一:

    NSData*datastr = [manager contentsAtPath:[documentsPath stringByAppendingPathComponent:@"test.txt"]];

//    把读取到的二进制数据转换成字符串

    NSString *string = [[NSString alloc]initWithData:datastr encoding:NSUTF8StringEncoding];

    NSLog(@"从文件中读取的内容是 %@",string);

//    3.2方式二

    NSString *datastring = [NSString stringWithContentsOfFile:[documentsPath stringByAppendingPathComponent:@"test.txt"] encoding:NSUTF8StringEncoding error:nil];

    NSLog(@"获取的文件内容是%@",datastring);

    

    

//    4.剪切文件

    NSString *moveAtpath = [documentsPath stringByAppendingPathComponent:@"test.txt"];

    NSString *toAtpath = [documentsPath stringByAppendingPathComponent:@"test2/file.txt"];

    BOOL ismove =[manager moveItemAtPath:moveAtpath toPath:toAtpath error:nil];

    if (ismove) {

        NSLog(@"移动成功");

        

    }

//    5.复制文件

    NSString *copyAtpath = [documentsPath stringByAppendingPathComponent:@"test2/file.txt"];

    NSString *copyTopath = [documentsPath stringByAppendingPathComponent:@"file2.txt"];

BOOL iscopy = [manager copyItemAtPath:copyAtpath toPath:copyTopath error:nil];

    if (iscopy) {

        NSLog(@"复制成功");

    }

    

//    6.删除文件

    NSString *removeAtpath = [documentsPath stringByAppendingPathComponent:@"test.txt"];

BOOL isremove = [manager removeItemAtPath:removeAtpath error:nil];

    if (isremove) {

        NSLog(@"移除成功");

    }

//    7.获取文件属性

   NSDictionary *attrDic =[manager attributesOfItemAtPath:documentsPath error:nil];

 

    NSLog(@"attrDic = %@",attrDic);

    /*

     

     NSFileCreationDate = "2016-01-22 08:24:09 +0000";

     NSFileExtensionHidden = 0;

     NSFileGroupOwnerAccountID = 80;

     NSFileGroupOwnerAccountName = admin;

     NSFileModificationDate = "2016-02-19 03:15:33 +0000";

     NSFileOwnerAccountID = 501;

     NSFilePosixPermissions = 493;

     NSFileReferenceCount = 6;

     NSFileSize = 204;

     NSFileSystemFileNumber = 4663817;

     NSFileSystemNumber = 16777218;

     NSFileType = NSFileTypeDirectory;

     */

    

//    8.获取当前文件夹下的所有子文件 利用manager 获得子文件的相对路径

    NSArray *subpaths = [manager subpathsAtPath:documentsPath];

    NSLog(@"subpath= %@",subpaths);

//    对上面的数组进行遍历 求出documents文件夹下的所有文件的大小

    float totalSize =0;

    for (NSString *subpath in subpaths) {

//      获取到的是相对路径,要进行拼接成绝对路径

        NSString *path = [documentsPath stringByAppendingPathComponent:subpath];

        

        NSDictionary *dic = [manager attributesOfItemAtPath:path error:nil];

        NSNumber *size = [dic objectForKey:NSFileSize];

        

        totalSize += [size floatValue];

        

    }

    NSLog(@"文件夹document的总大小是 :%.2f",totalSize);

    

}

 

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end

 

posted on 2016-02-19 11:39  2015ztlucky  阅读(156)  评论(0编辑  收藏  举报