URL,data(网络下载,url编码,文件读写),NSFileManager(文件管理者,系统单例)

本文介绍了如何在iOS应用中使用NSFileManager进行文件管理,包括创建、删除文件夹,判断文件夹是否存在,以及如何将字符串、数组和字典等数据类型写入文件。同时,还介绍了如何计算文件夹大小及删除指定文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//获取工程的根目录

NSString *rootPath = NSHomeDirectory();

//获取Documents、library、tmp目录的方法:

// 获取Documents的目录的方法:

NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSLog(@"documentsPath = %@",documentsPath);

// 获取Library目录的方法:

NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSLog(@"libraryPath = %@",libraryPath);

// 获取tmp目录的方法:(比较简单)

NSTemporaryDirectory();
NSString *tmpPath = NSTemporaryDirectory();
NSLog(@"tmpPath = %@",tmpPath);

//创建一个文件管理者(系统单例对象)
这个对象可以进行文件夹的操作(增、删、判断文件夹是否存在 ,还可以进行文件的写入)

NSFileManager *fileManager = [NSFileManager defaultManager];
#pragma mark - NSFileManager 文件管理者,系统单例
//(增、删、判断文件夹是否存在 ,还可以进行文件的写入)
-(void)fileManager
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
//    增文件夹:
   BOOL isSuccess = [fileManager createDirectoryAtPath:@"想要创建的目标路径" withIntermediateDirectories:YES attributes:nil error:nil];
//    减文件夹:
    isSuccess =[fileManager removeItemAtPath:@"想要删除的目标路径" error:nil];
//    判断文件夹是否存在:
    isSuccess = [fileManager isExecutableFileAtPath:@"想要判断是否存在的文件夹的目录"];
//    文件的写入,    data也有类似的功能
//    contents:  这个参数就是填 要写入文件的data二进制数据了
    /*
    isSuccess = [fileManager createFileAtPath:@"想要写入的文件的路径(包括后面的名字)" contents:(nullable NSData *) attributes:nil];
    */

}     

字符串、数组、字典写入本地文件,获取文件夹下文件的目录数组,计算文件夹的大小,删除文件

//    将字符串写入到本地
    NSString * targetString = @"HELLO GUYS";

    BOOL flag = [targetString writeToFile:[imgsTmpPath stringByAppendingPathComponent:@"a.txt"] atomically:YES encoding:NSUTF8StringEncoding error:nil];
    if (flag)
    {
        NSLog(@"写入 OK ");
    } else {
        NSLog(@"写入 不OK");
    }

//    数组写入本地
    NSArray *array = @[@"六",@"王",@"流",@"刘"];
    flag = [array writeToFile:[imgsTmpPath stringByAppendingPathComponent:@"array.txt"] atomically:YES];

    if (flag)
    {
         NSLog(@"写入数组成功 ");
    }else
    {
         NSLog(@"写入数组失败");
    }
//    将字典写入本地
    NSDictionary *dic = @{@"name":@"Rick",@"age":@25,@"address":@"GZ"};

    flag = [dic writeToFile:[imgsTmpPath stringByAppendingPathComponent:@"dic.txt"] atomically:YES];

    if (flag)
    {
        NSLog(@"写入字典成功 ");
    }else
    {
        NSLog(@"写入字典失败");
    }

//    计算文件的大小
//    获得将要计算的文件夹
    NSFileManager *fileManager = [NSFileManager defaultManager];
//    获得imgs目录下文件名组成的数组

    NSArray *imgsFileArray = [fileManager subpathsAtPath:imgsTmpPath];

    NSLog(@"imgsFileArray = %@",imgsFileArray);
    CGFloat count = 0;
    for (NSString *ele in imgsFileArray)
    {
        NSData *data = [NSData dataWithContentsOfFile:[imgsTmpPath stringByAppendingPathComponent:ele]];

        count += data.length;

    }
    count = count/1024/1024;
    NSLog(@"缓存文件的大小为: %.2fM",count);

//    删除文件

    for (NSString *ele in imgsFileArray)
    {
        BOOL isSuccess = [fileManager removeItemAtPath:[imgsTmpPath stringByAppendingPathComponent:ele] error:nil];
        if (isSuccess)
        {
            NSLog(@"删除成功");
        } else {
            NSLog(@"删除失败");
        }


    }

声明:

1.以上内容属于本人整理的笔记,如有错误的地方希望能告诉我,大家共同进步。

2.以上内容有些段落或语句可能是本人从其他地方Copy而来,如有侵权,请及时告诉我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值