将字符串 数组 字典写入本地文件,并计算文件的大小,最后删除文件

iOS文件操作实践
本文介绍了一个iOS应用中关于文件操作的具体实现,包括文件夹的创建、不同数据类型的文件写入、文件大小计算及文件删除等功能,并展示了如何使用Objective-C进行实际操作。
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //首先封装一个函数,使得这个函数返回的是我们在指定目录下想要的文件夹的路径
    //创建了一个Imgs文件夹
    NSString *imgsTmpPath = [self creatDirInTmp:@"Imgs"];
    
    NSLog(@"imgsTmpPath = %@",imgsTmpPath);

    //将字符串写入本地文件
    NSString *targetString = @"Hello guys";
   BOOL flag = [targetString writeToFile:[imgsTmpPath stringByAppendingPathComponent:@"a.txt"] atomically:YES encoding:NSUTF8StringEncoding error:nil];
    if (flag)
    {
        NSLog(@"字符串写入成功");
    }
    else
    {
        NSLog(@"字符串写入失败");
    }
    //将数组写入本地文件
    NSArray *array = @[@"张国荣",@"Beyond",@"张学友",@"陈奕迅",@"信乐团"];
   BOOL flag1 = [array writeToFile:[imgsTmpPath stringByAppendingPathComponent:@"arrary.txt"] atomically:YES];
    if (flag1)
    {
        NSLog(@"数组写入成功");
    }
    else
    {
        NSLog(@"数组写入失败");
    }
    //将字典写入本地
    NSDictionary *dictionary = @{@"name":@"Rick",@"age":@"25",@"address":@"Gz"};
   BOOL flag2 = [dictionary writeToFile:[imgsTmpPath stringByAppendingPathComponent:@"dictionary.txt"] atomically:YES];
    if (flag2)
    {
        NSLog(@"字典写入本地成功");
    }
    else
    {
        NSLog(@"字典写入本地失败");
    }
    //计算文件大小
    //获得将要计算的文件夹
   NSFileManager *fileManager = [NSFileManager defaultManager];
    //获得imgs目录下文件名组成的数组
    NSArray *imgsFileArray = [fileManager subpathsAtPath:imgsTmpPath];
    NSLog(@"imgsFileArray = %@",imgsFileArray);
    /*打印结果:
     imgsFileArray = (
     "a.txt",
     "arrary.txt",
     "dictionary.txt"
     */
    //遍历数组
    CGFloat count = 0;
    for (NSString *ele in imgsFileArray)
    {
        NSData *data = [NSData dataWithContentsOfFile:[imgsTmpPath stringByAppendingPathComponent:ele]];
        count += data.length;
    }
    count = count/1024/1024;//将字节转化成M.
    NSLog(@"缓存文件的大小为:%.2fM",count);
    //删除文件
    for (NSString *ele in imgsFileArray)
    {
       BOOL isSuccess = [fileManager removeItemAtPath:[imgsTmpPath stringByAppendingPathComponent:ele] error:nil];
        if (isSuccess)
        {
            NSLog(@"删除成功");
        }
        else
        {
            NSLog(@"删除失败");
        }
        
    }

    
    
}
//这里我们封装一个函数,使得这个函数返回的是我们在tmp目录下想要的文件夹的路径
-(NSString *)creatDirInTmp:(NSString *)dirName
{
    //获取根目录文件
    NSString *tmp = NSTemporaryDirectory();
    
    NSString *dirPath = [tmp stringByAppendingPathComponent:dirName];
   NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    if (![fileManager fileExistsAtPath:dirPath])
    {
       BOOL isSuccess = [fileManager createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:&error];
        if (!isSuccess)
        {
            dirPath = nil;
            NSLog(@"error = %@",error.debugDescription);
        }
    }
    return dirPath;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值