#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