天天看点

NSFileManager文件管理器, 沙盒SandBox

NSFileManager,顾名思义,文件管理器,在我们开发项目的过程中经常使用,如想要把一些网络请求下来的数据放入本地等,都要通过它进行管理

下面将介绍NSFileManager的一些基本用法(本文都写在viewDidLoad中,可根据自己的需求写)

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

#pragma mark -- NSFileManager 文件管理器

#pragma mark 1.创建文件夹

    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

   NSString *path =  [array lastObject];

    NSString *filePath = [path stringByAppendingPathComponent:@"xiaoxin"];

    // 1.创建文件管理器对象

    NSFileManager *fileManager = [NSFileManager defaultManager];

#warning   2.创建文件夹

    [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];

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

    NSString *str = @"小新爱吃甜不辣";

    NSString *filePathZhaoY = [filePath stringByAppendingPathComponent:@"xiaoxin.txt"];

#warning 重点 写入本地 writeToFile

    [str writeToFile:filePathZhaoY atomically:YES encoding:NSUTF8StringEncoding error:nil];

    NSString *filePath1 = [path stringByAppendingPathComponent:@"nini"];

    [fileManager createDirectoryAtPath:filePath1 withIntermediateDirectories:YES attributes:nil error:nil];

    NSString *filePathJCZ = [filePath1 stringByAppendingPathComponent:@"nini.txt"];

    NSString *str1 = @"妮妮爱玩家家酒";

    [str1 writeToFile:filePathJCZ atomically:YES encoding:NSUTF8StringEncoding error:nil];

    // 将xiaoxin文件夹下得xiaoxin.txt 移动到nini文件夹

    NSString *filePathJCZT = [filePath1 stringByAppendingPathComponent:@"xiaoxin2.txt"];

  // BOOL result = [fileManager moveItemAtPath:filePathZhaoY toPath:filePathJCZT error:nil];

//    if (result) {

//        NSLog(@"移动成功");

//    }else

//    {

//        NSLog( @"移动失败");

//    }

    // 拷贝文件到另一个目录中

  BOOL result1 =  [fileManager copyItemAtPath:filePathZhaoY toPath:filePathJCZT error:nil];

    if (result1) {

        NSLog(@"拷贝成功");

    }else

    {

        NSLog(@"拷贝失败");

    }

#warning 重点 删除文件

       BOOL result2 = [fileManager removeItemAtPath:filePathZhaoY error:nil];

    if (result2) {

        NSLog(@"删除成功");

    }

    else

    {

        NSLog(@"删除失败");

    }

#warning 重点  判断文件是否存在

    BOOL result3 = [fileManager fileExistsAtPath:filePathJCZT];

    if (result3) {

        NSLog(@"文件存在");

    }else

    {

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

    }

}

2. 沙盒SandBox基本使用方法

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

#pragma mark -- 获取沙盒路径

    // 参数1.获取路径的参数

    // 参数2.搜索路径范围

    // 参数3.yes代表绝对路径 no代表相对路径

    // 获取document目录

    // NSDocumentDIrectory : 获取document目录

    // NSCacheDIrectory : 获取caches目录

    // NSLibraryDIrectory : 获取library目录

   NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

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

    NSString *documentStr = [array lastObject];

    NSLog(@"str = %@", documentStr);

    NSString *homeDirectories = NSHomeDirectory();

    NSLog(@"获取沙盒根目录 = %@", homeDirectories);

#pragma mark -- 在沙盒路径下创建文件

    // 1.拼接文件时自动生成'/'

    NSString *documentsPath = [documentStr stringByAppendingPathComponent:@"student.xml"];

    NSLog(@"documentPath  %@", documentsPath);

    // 2.拼接文件是直接拼接在路径名后面

    NSString *documentsPath1 = [documentStr stringByAppendingString:@"/tea.plist"];

    NSLog(@"documentPath1 = %@", documentsPath1);

    //3.在路径后面加'.'

    NSString *documnetPath2 = [documentStr stringByAppendingPathExtension:@"大水杯.xml"];

    NSLog(@"documentPath2 = %@", documnetPath2);

   // NSString *str = @"大书包";

   // [str writeToFile:documentsPath atomically:YES encoding:NSUTF8StringEncoding error:nil];

    // 将数组写入本地路径

    NSArray *array1 = [NSArray arrayWithObjects:@"大水杯", @"大伞兵", @"大书本", nil];

    [array1 writeToFile:documentsPath atomically:YES];

    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"大水杯", @"name", @"20", @"age", @"男", @"sex", nil];

    // 1.将字典写入本地plis文件

    [dic writeToFile:documentsPath1 atomically:YES];

    //2.取出plist文件

    NSDictionary *fileDic = [NSDictionary dictionaryWithContentsOfFile:documentsPath1];

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

   // 3.修改字典的值

    [fileDic setValue:@"22" forKey:@"name"];

    // 取出修改后

    NSDictionary *fileDic1 = [NSDictionary dictionaryWithContentsOfFile:documentsPath1];

    NSLog(@"fileDic = %@", fileDic1);

    // 4.再写一次

    [fileDic writeToFile:documentsPath1 atomically:YES];

    //5. 验证取出修改后的

    NSDictionary *fileDic3 = [NSDictionary dictionaryWithContentsOfFile:documentsPath1];

    NSLog(@"fileDic = %@", fileDic3);

}

继续阅读