1.计算图片的缓存
#define kDocument_Folder1 [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/com.hackemist.SDWebImageCache.default"]//图片缓存
#define kDocument_Folder2 [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/com.qianfeng.Travel"]
- (NSString *)cacheCalculate {
NSArray *URl = @[kDocument_Folder1,kDocument_Folder2];//沙盒路径
NSNumber *total;
float totalSize;
for(int i = 0;i < 2; i++){
//1.先创建一个文件管理类NSFileManager的对象(单例类)
NSFileManager *fM = [NSFileManager defaultManager];
//2.浅度遍历->只读第一层
//第一个参数:路径:一般绝对路径(/开头)
//Users/mac/Desktop/zz1409/zz1409上课
NSArray *fileList = [fM contentsOfDirectoryAtPath:URl[i] error:nil];
NSString *path;
const unsigned int bytes = 1024*1024 ;//字节(兆)
float size = 0.0;
for(NSString *file in fileList) {
path = [URl[i] stringByAppendingPathComponent:file];
//http://blog.sina.com.cn/s/blog_768552710101e5vl.html
//attributesOfItemAtPath:方法的功能是获取文件的大小、文件的内容等属性 .是个字典
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
size=size+(float)[fileAttributes fileSize];//计算大小
}
//累加所有沙盒路径的字节大小
totalSize += size/bytes;
//转成NSNumber类型
total = [NSNumber numberWithFloat:totalSize];
}
//http://www.cnblogs.com/yingkong1987/archive/2013/03/11/2953774.html
//数字的格式化
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
//设置10进位格式
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
//http://small.qiang.blog.163.com/blog/static/97849307201182224334143/
//
[formatter setPositiveFormat:@"##0.0"];
return [NSString stringWithFormat:@"%@MB",[formatter stringFromNumber:total]] ;
}
if (indexPath.section == 0 && indexPath.row == 3) {
// NSString *cafFilePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
NSFileManager* fileManager=[NSFileManager defaultManager];
[fileManager removeItemAtPath:kDocument_Folder1 error:nil];
[fileManager removeItemAtPath:kDocument_Folder2 error:nil];
[_tableView reloadData];
}
2.其他的操作
1⃣️文件管理操作&文件创建
//文件管理操作:先创建一个文件管理类(NSFileManager)的对象
//1.创建文件管理类对象:NSFileManager是一个单例类
NSFileManager *manager = [NSFileManager defaultManager];
//2.遍历
//2.1浅度遍历 --> 只读第一层
//第一个参数:路径:一般绝对路径(/开头)
// /Users/mac/Desktop/zz1409/zz1409上课
NSArray *array = [manager contentsOfDirectoryAtPath:@"/Users/mac/Desktop/zz1409/zz1409上课" error:nil];
NSLog(@"array = %@",array);
//2.2深度遍历
NSArray *arr2 = [manager subpathsOfDirectoryAtPath:@"/Users/mac/Desktop/zz1409/zz1409上课" error:nil];
NSLog(@"深度遍历arr2 = %@",arr2);
//获取/Users/lzxuan/Desktop/音乐播放器素材下得所有mp3文件,并且打印全路径
//遍历数组
for (NSString *fileName in arr2) {
#if 0
if ([fileName hasSuffix:@".mp3"]) {
NSLog(@"name:%@",fileName);
NSString *filePath = [path stringByAppendingFormat:@"/%@",fileName];
NSLog(@"path:%@",filePath);
}
#else
NSString *name = [fileName pathExtension];//获取文件扩展名
if ([name isEqualToString:@"mp3"]) {
NSLog(@"name:%@",fileName);
//官方 自带的 路径拼接函数
NSString *filePath = [path stringByAppendingPathComponent:fileName];
NSLog(@"path:%@",filePath);
}
#endif
}
//2.创建文件
//2.1创建一个文件
//第一个参数:路径:路径要有要创建的文件的名字
//第二个参数:内容 --> NSData类类型的对象
//第三个参数:属性 --> 一般写nil,使用默认的属性
NSString *str = @"I am a student";
//NSString --> NSData(类)
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
BOOL ret = [manager createFileAtPath:@"/Users/mac/Desktop/zz1409/zz1409上课/1.m" contents:data attributes:nil];
if (ret) {
NSLog(@"创建成功");
}
else{
NSLog(@"创建失败");
}
//创建文件夹
/*
返回值:成功yes 失败 no
第一个参数:目标文件夹路径
二 :YES :不管中间目录是否存在 都会创建目标文件夹(如果中间目录不存在那么中间目录也会创建)
NO: 如果中间的目录不存在那么不创建
如果目标文件夹 存在 ,那么不创建新的,会创建失败
三 文件属性
四 错误信息
*/
NSError *error = nil;
BOOL ret2 = [manager createDirectoryAtPath:@"/Users/mac/Desktop/zz1409/zz1409上课/111/22" withIntermediateDirectories:YES attributes:nil error:&error];
if (ret2) {
NSLog(@"目录创建成功");
}
else{
NSLog(@"目录创建失败");
}
//3.判断指定路径下是否已经存在某个文件(或目录) --> 创建文件时,可先判断指定路径是否已经有该文件(或目录),如果有,就不再创建,否则则创建
BOOL ret3 = [manager fileExistsAtPath:@"/Users/mac/Desktop/zz1409/zz1409上课/111"];
if (ret3) {
NSLog(@"已存在");
}
else{
NSLog(@"不存在");
}
// //判断目录是否存在
// BOOL isDirectory = YES;
// BOOL ret4 = [manager fileExistsAtPath:@"/Users/mac/Desktop/zz1409/zz1409上课/111" isDirectory:&isDirectory];
// if (ret4) {
// NSLog(@"已存在该目录");
// }
// else{
// NSLog(@"不存在该目录");
// }
2⃣️拷贝 移动 删除
//文件管理操作,先要获取到文件管理类(NSFileManager)的对象
NSFileManager *manager = [NSFileManager defaultManager];
//1.拷贝
//第一个参数:要拷贝的文件路径
//第二个参数:要拷贝到的路径(包括要拷贝的文件的名字)
BOOL ret0 = [manager copyItemAtPath:@"/Users/mac/Desktop/zz1409/zz1409上课/1.m" toPath:@"/Users/mac/Desktop/zz1409/zz1409上课/111/1.m" error:nil];
if (ret0) {
NSLog(@"拷贝成功");
}
else{
NSLog(@"拷贝失败");
}
//2.移动(重命名)
//第一个参数:要移动的文件路径
//第二个参数:要移动到的路径(包括要移动的文件的名字(名字可以不是1.m))
BOOL ret1 = [manager moveItemAtPath:@"/Users/mac/Desktop/zz1409/zz1409上课/1.m" toPath:@"/Users/mac/Desktop/zz1409/zz1409上课/day06/333.m" error:nil];
if (ret1) {
NSLog(@"移动成功");
}
else{
NSLog(@"移动失败");
}
//3.删除:永久性删除(删除后不会出现在垃圾篓里),谨慎删除
//第一个参数:要删除的文件的路径
BOOL ret2 = [manager removeItemAtPath:@"/Users/mac/Desktop/zz1409/zz1409上课/day06/333.m" error:nil];
if (ret2) {
NSLog(@"删除成功");
}
else{
NSLog(@"删除失败");
}
//4.获取文件的属性
//第一个参数:文件路径
NSDictionary *attributeDict = [manager attributesOfItemAtPath:@"/Users/mac/Desktop/zz1409/zz1409上课/111/1.m" error:nil];
NSLog(@"属性dict = %@",attributeDict);
//获取文件大小:第一种方法
NSNumber *size = [attributeDict objectForKey:NSFileSize];
NSLog(@"size = %@",size);
int intSize = [size intValue];
NSLog(@"intSize = %d",intSize);
long long fileSize1 = [[dict objectForKey:@"NSFileSize"] longLongValue];
NSLog(@"%lld",fileSize1);
//获取文件大小:第二种方法
unsigned long long fileSize = [attributeDict fileSize];
NSLog(@"fileSize = %llu",fileSize);
}
3⃣️ 读 写 更新 操作
//要想进行文件的数据操作,需要使用到文件数据操作类NSFileHandle
//NSFileHandle:文件句柄类
//三种方式:reading writing updating
//reading:只能对这个文件进行读的操作
//writing:只能对这个文件进行写的操作
//updating:对这个文件可读可写
//1.reading
//第一个参数:要操作的文件的路径/Users/mac/Desktop/zz1409/zz1409上课/111/1.m
NSFileHandle *handle0 = [NSFileHandle fileHandleForReadingAtPath:@"/Users/mac/Desktop/zz1409/zz1409上课/111/1.m"];
//2.writing
NSFileHandle *handle1 = [NSFileHandle fileHandleForWritingAtPath:@"/Users/mac/Desktop/zz1409/zz1409上课/111/1.m"];
//3.updating
NSFileHandle *handle2 = [NSFileHandle fileHandleForUpdatingAtPath:@"/Users/mac/Desktop/zz1409/zz1409上课/111/1.m"];
4⃣️读
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:@"/Users/mac/Desktop/zz1409/zz1409上课/111/1.m"];
//读文件:
//1.读指定长度的文件:读的同时,会把文件内容的偏移量定位到读的长度的位置
NSData *data = [handle readDataOfLength:4];
NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"str = %@",str);
//第二个读文件时,从上一次的偏移到的位置开始读
NSData *data1 = [handle readDataOfLength:4];
NSString *str1 = [[NSString alloc]initWithData:data1 encoding:NSUTF8StringEncoding];
NSLog(@"str1 = %@",str1);
//2.读到文件尾
NSData *data2 = [handle readDataToEndOfFile];
NSString *str2 = [[NSString alloc]initWithData:data2 encoding:NSUTF8StringEncoding];
NSLog(@"读到文件尾:%@",str2);
//3.设置文件的偏移量:
//3.1指定位置
[handle seekToFileOffset:0];
NSData *data3 = [handle readDataToEndOfFile];
NSString *str3 = [[NSString alloc]initWithData:data3 encoding:NSUTF8StringEncoding];
NSLog(@"偏移量设置为0后:%@",str3);
//3.2把文件偏移量定位到文件尾
[handle seekToEndOfFile];
5⃣️ 写
//写文件
NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:@"/Users/mac/Desktop/zz1409/zz1409上课/111/1.m"];
//读写操作的数据格式都是NSData类型的
NSString *str = @"when I was young";
//NSString --> NSData
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
//写
//要想原来的内容不被覆盖,需要先把文件的偏移量定位到文件尾
[handle seekToEndOfFile];
//文件截断:截断到给定的参数的长度
[handle truncateFileAtOffset:20];
[handle writeData:data];
//写操作完成之后,同步磁盘 --> 把缓冲区里面的内容立即写入磁盘
[handle synchronizeFile];