iOS沙盒机制和文件(三)

1. 判断一个文件是否存在, 传入全路径(fileExistsAtPath)

    // 创建文件管理器
    NSFileManager * fileManager = [NSFileManager defaultManager];

    NSString * documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString * filePath = [documents stringByAppendingPathComponent:@"test"];

    // 判断一个文件是否存在,传入全路径
    if ([fileManager fileExistsAtPath:filePath]) {
        NSLog(@"it is exit");
    }

2. 在Documents里创建目录

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSLog(@"documentsDirectory%@",documentsDirectory);
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *testDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"];
    // 创建目录
    [fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];

3. 在目录下创建文件

    NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test00.txt"];
    NSString *testPath2 = [testDirectory stringByAppendingPathComponent:@"test22.txt"];
    NSString *testPath3 = [testDirectory stringByAppendingPathComponent:@"test33.txt"];

    NSString *string = @"写入内容,write String";
    [fileManager createFileAtPath:testPath contents:[string  dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    [fileManager createFileAtPath:testPath2 contents:[string  dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    [fileManager createFileAtPath:testPath3 contents:[string  dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];

###4. 获取目录列里所有文件名

    // 两种方法获取:subpathsOfDirectoryAtPath 和subpathsAtPath
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSLog(@"documentsDirectory%@",documentsDirectory);
    NSFileManager *fileManage = [NSFileManager defaultManager];
    NSString *myDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"];
    NSArray *file = [fileManage subpathsOfDirectoryAtPath: myDirectory error:nil];
    NSLog(@"%@",file);
    NSArray *files = [fileManage subpathsAtPath: myDirectory];
    NSLog(@"%@",files);

5. fileManager使用操作当前目录

    // 创建文件管理器
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    // 更改到待操作的目录下
    [fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];
    // 创建文件fileName文件名称,contents文件的内容,如果开始没有内容可以设置为nil,attributes文件的属性,初始为nil
    NSString * fileName = @"testFileNSFileManager.txt";
    NSArray *array = [[NSArray alloc] initWithObjects:@"hello world",@"hello world1", @"hello world2",nil];
    [fileManager createFileAtPath:fileName contents:array attributes:nil];

6. 删除文件

    [fileManager removeItemAtPath:fileName error:nil];

7. 移动文件

    NSFileManager *mgr = [NSFileManager defaultManager];
    // 方式一
    [mgr moveItemAtURL:atURL toURL:toURL error:nil];
    // 方式二
    [mgr moveItemAtPath:atPath toPath:toPath error:nil];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS和安卓的沙盒是用来保护应用程序数据的安全性的一种机制。它们的主要区别如下: 1. 安全性:iOS沙盒机制相对安卓更加严格。iOS要求应用程序在自己的沙盒内运行,限制了应用之间的直接访问和交互。而安卓的沙盒机制相对较弱,应用程序可以更容易地访问和共享数据。 2. 文件系统结构:iOS沙盒机制将每个应用程序的文件分隔为多个目录,包括应用程序包、文档目录、缓存目录等。每个目录只能由特定的应用程序进行访问和写入。而安卓的沙盒机制通常是基于应用的用户ID,并且应用程序可以自由地向沙盒中的文件系统写入和访问。 3. 权限管理:iOS沙盒机制通过权限管理来限制应用程序对系统资源的访问。应用程序需要在安装时声明所需的访问权限,并由用户在使用时授予。而安卓的沙盒机制在应用程序安装时会一次性获取所有权限,并且用户只能在软件设置中进行修改。 4. 应用程序的更新:iOS沙盒机制要求每个应用程序都在单独的容器中运行,因此应用程序的更新通常是通过替换整个应用程序包来完成的。而安卓的沙盒机制允许应用程序在更新时只替换其中的部分文件,从而减少下载和更新时间。 综上所述,iOS和安卓的沙盒机制在安全性、文件系统结构、权限管理和应用程序更新方面存在一些差异。iOS沙盒机制相对更严格,保护了应用程序和用户数据的安全性,但也可能限制了应用程序之间的交互。而安卓的沙盒机制相对较弱,允许应用程序更自由地访问和共享数据,但也可能增加了安全隐患。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值