NSFileManager文件管理器、NSFileHandle文件句柄 - iOS-目录(系统目录/指定路径子目录的创建)、文件的创建和删除、文件的读取和写入

  • 一、.m类里头文件如下
#define DOWNLOAD_URL @"http://localhost:8080/iOSDemo/app_ios.zip"
#define DOWNLOAD_URL_TEST @"/Users/nixinsheng/Library/Developer/CoreSimulator/Devices/9BA7EC04-1CAD-4026-A1BA-C9B851FCE6B7/data/Containers/Data/Application/CC318755-19A8-484C-B612-A621F2F8E01A/Library/Caches/iOSDemo/file"
  • 二、ViewDidLoad里代码示例 「NSFileManager文件管理器、NSFileHandle文件句柄」
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"---DOWNLOAD_URL:%@",DOWNLOAD_URL);
    NSString *filename = [DOWNLOAD_URL lastPathComponent];
    NSLog(@"---fileName:%@",filename);
    NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
    NSLog(@"---Library/Caches:%@",path);
    NSString *fullpath = [path stringByAppendingPathComponent:filename];
    NSLog(@"---fullpath:%@",fullpath);
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error = nil;
    //判断当前目录是否存在
    BOOL isExist = [fileManager contentsOfDirectoryAtPath:DOWNLOAD_URL_TEST error:&error];
    NSLog(@"---isExist:%d",isExist);
    //创建子目录
    BOOL createDirectory = [fileManager createDirectoryAtPath:DOWNLOAD_URL_TEST withIntermediateDirectories:YES attributes:nil error:&error];
    NSLog(@"---createDirectory:%d",createDirectory);
    
    //判断目标路径下的目标文件 是否存在
    if (![fileManager fileExistsAtPath:[DOWNLOAD_URL_TEST stringByAppendingPathComponent:@"smartposapp_ios.zip"]]) {
        //创建一个空的文件到沙盒中
        //在指定路径下创建文件内容
        //如果没有下载文件的话,就创建一个文件。如果有下载文件的话,则不用重新创建(不然会覆盖掉之前的文件)
        [fileManager createFileAtPath:[DOWNLOAD_URL_TEST stringByAppendingPathComponent:@"smartposapp_ios.zip"] contents:nil attributes:nil];
        NSLog(@"---文件创建完成:%@",[DOWNLOAD_URL_TEST stringByAppendingPathComponent:@"smartposapp_ios.zip"]);
    }else{
        NSLog(@"---文件已存在");
    }
    
    BOOL removeItemAtPath = [fileManager removeItemAtPath:[DOWNLOAD_URL_TEST stringByAppendingPathComponent:@"smartposapp_ios.zip"] error:&error];
    NSLog(@"---removeItemAtPath:%d",removeItemAtPath);
    
    //2、文件的读取和写入
    NSFileHandle *handleRead = [NSFileHandle fileHandleForReadingAtPath:[DOWNLOAD_URL_TEST stringByAppendingPathComponent:@"nixs.txt"]];
    //读取文件内容-直到结尾
    NSData *fileData = [handleRead readDataToEndOfFile];
    NSString *fileStr = [[NSString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];
    NSLog(@"---fileStr:%@",fileStr);
    //关闭文件
    [handleRead closeFile];
    
    
    NSFileHandle *handleWrite = [NSFileHandle fileHandleForWritingAtPath:[DOWNLOAD_URL_TEST stringByAppendingPathComponent:@"nixs.txt"]];
    NSData *dataW = [@"\n加菲猫 2号 2021年01月20日16:03:18" dataUsingEncoding:NSUTF8StringEncoding];
    //定位到文件最后
    [handleWrite seekToEndOfFile];
    //写入文件
    [handleWrite writeData:dataW];
    //关闭文件
    [handleWrite closeFile];
 
}
  • 三、运行结果
2021-01-20 16:07:18.812529+0800 AFNDemo_Example[15638:180053] ---DOWNLOAD_URL:http://localhost:8080/iOSDemo/app_ios.zip
2021-01-20 16:07:18.812747+0800 AFNDemo_Example[15638:180053] ---fileName:app_ios.zip
2021-01-20 16:07:18.812946+0800 AFNDemo_Example[15638:180053] ---Library/Caches:/Users/nixinsheng/Library/Developer/CoreSimulator/Devices/9BA7EC04-1CAD-4026-A1BA-C9B851FCE6B7/data/Containers/Data/Application/A13130AC-C2F0-4574-ACFD-2D2BE8BAA35B/Library/Caches
2021-01-20 16:07:18.813098+0800 AFNDemo_Example[15638:180053] ---fullpath:/Users/nixinsheng/Library/Developer/CoreSimulator/Devices/9BA7EC04-1CAD-4026-A1BA-C9B851FCE6B7/data/Containers/Data/Application/A13130AC-C2F0-4574-ACFD-2D2BE8BAA35B/Library/Caches/sapp_ios.zip
2021-01-20 16:07:18.813552+0800 AFNDemo_Example[15638:180053] ---isExist:1
2021-01-20 16:07:18.814022+0800 AFNDemo_Example[15638:180053] ---createDirectory:1
2021-01-20 16:07:18.815018+0800 AFNDemo_Example[15638:180053] ---文件创建完成:/Users/nixinsheng/Library/Developer/CoreSimulator/Devices/9BA7EC04-1CAD-4026-A1BA-C9B851FCE6B7/data/Containers/Data/Application/CC318755-19A8-484C-B612-A621F2F8E01A/Library/Caches/iOSDemo/file/app_ios.zip
2021-01-20 16:07:18.815939+0800 AFNDemo_Example[15638:180053] ---removeItemAtPath:1
2021-01-20 16:07:18.816836+0800 AFNDemo_Example[15638:180053] ---fileStr:加菲猫 2021年01月20日15:57:22

加菲猫 2号 2021年01月20日16:03:18加菲猫 2号 2021年01月20日16:03:18
加菲猫 2号 2021年01月20日16:03:18

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值