- void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 文件管理器
// NSFileManager 是系统的单例
NSFileManager *manager = [NSFileManager defaultManager];
NSString *document = [NSSearchPathForDirectoriesInDomainsNSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *lanouFilePath = [document stringByAppendingPathComponent:@"lanou"];
// 创建文件夹
// 参数1: 文件夹路径
// 参数2: 是否创建父文件夹 一般都是yes ,不然会出错
// 参数3: 设置属性
// 参数4: 错误信息
BOOL result = [manager createDirectoryAtPath:lanouFilePath withIntermediateDirectories:YES attributes:nil error:nil];
NSLog@”%@”, lanouFilePath);
if result) {
NSLog@”创建文件夹成功”);
} else {NSLog@"创建文件夹错误");
}
NSString *lanouFile = [lanouFilePath stringByAppendingPathComponent:@"蓝鸥.txt"];
NSString *str = @"蓝鸥1111";
[str writeToFile:lanouFile atomically:YES encoding:NSUTF8StringEncoding error:nil];
// 移动到 “班长”文件夹 里 “班长.txt” 文件中
NSString *banZhangPath = [document stringByAppendingPathComponent:@”班长”];
NSString *banzhangFile = [banZhangPath stringByAppendingPathComponent:@”班长.txt”];
[manager createDirectoryAtPath:banZhangPath withIntermediateDirectories:YES attributes:nil error:nil];
// 移动文件到指定路径下
[manager moveItemAtPath:lanouFile toPath:banzhangFile error:nil];
//******
// 复制文件到指定文件中
[manager copyItemAtPath:banzhangFile toPath:lanouFile error:nil];
// 删除 文件
[manager removeItemAtPath:lanouFile error:nil];
// 删除 文件夹
[manager removeItemAtPath:lanouFilePath error:nil];
// 判断文件是否存在
BOOL result3 = [manager fileExistsAtPath:banzhangFile];
if result3) {
NSLog@”文件存在”);
}
// *****************************************
//清除文件夹
long size = [self fileSizeAtPath:document];
NSLog@”size = %ld”,size);
// float fSize = [self folderSizeAtPath:documentPath];
// NSLog@”fSize = %f”,fSize);
//
// [self clearCacheFlies];
// float fSize1 = [self folderSizeAtPath:documentPath];
// NSLog@”fSize = %f”,fSize1);
}
//一、清理缓存Document文件夹,删除文件方法
-void)clearCacheFlies{
NSString *cachPath = [NSSearchPathForDirectoriesInDomainsNSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
//获得document下的所有子文件夹
NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
NSLog@"files :%ld",[files count]);
NSLog@"files :%@",files);for NSString *p in files) {NSError *error;NSString *path = [cachPath stringByAppendingPathComponent:p];if [[NSFileManager defaultManager] fileExistsAtPath:path]) {[[NSFileManager defaultManager] removeItemAtPath:path error:&error];}NSLog@"清理成功!");
}
}
- long) fileSizeAtPath:NSString*) filePath{
NSFileManager* manager = [NSFileManager defaultManager];
if[manager fileExistsAtPath:filePath]){
//获取文件属性,根据属性计算大小
return[[manager attributesOfItemAtPath:filePath error:nil] fileSize];
}
return 0;
}
//遍历文件夹获得文件夹大小,返回多少M -
float ) folderSizeAtPath:NSString*) folderPath{
NSFileManager* manager = [NSFileManager defaultManager];
if ![manager fileExistsAtPath:folderPath])
{
return 0;
}//通过objectEnumerator获得枚举器
//NSEnumerator类似于NSArray,一般不单独使用。只是作为 NSArray,NSSet 和 NSDictionary 的辅助类(它们都有相应的方法转换为NSEnumerator)。特别要注意 NSEnumerator 中的元素一旦遍历过之后就不能重新遍历,必须重新创建一个NSEnumeratorNSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
NSString* fileName;
long long folderSize = 0;
while fileName = [childFilesEnumerator nextObject]) != nil){
NSLog@”fileName === %@”,fileName);
NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
folderSize += [self fileSizeAtPath:fileAbsolutePath];
}
return folderSize/1024.0*1024.0);
}
@end