先看OC获取文件大小,清除缓存,删除沙盒里文件的代码
// 显示缓存大小
+ (float)cacheSize {
NSString *cachPath = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES) firstObject];
return [self folderSizeAtPath:cachPath];
}
//计算一下单个文件的大小
+ (long 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 ;
NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath :folderPath] objectEnumerator ];
NSString * fileName;
long long folderSize = 0 ;
while ((fileName = [childFilesEnumerator nextObject]) != nil ){
NSString * fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
folderSize += [self fileSizeAtPath:fileAbsolutePath];
}
return folderSize / (1024.0 * 1024.0);
}
// 清理缓存
+ (void)clearCache {
NSString *cachPath = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES ) firstObject ];
NSArray *files = [[ NSFileManager defaultManager] subpathsAtPath:cachPath];
for (NSString *p in files) {
NSError *error = nil ;
NSString *path = [cachPath stringByAppendingPathComponent:p];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
[[NSFileManager defaultManager] removeItemAtPath:path error:&error];
}
}
}
// 删除沙盒里的文件
+ (void)deleteFileWithFilePath:(NSString *)filePath {
NSFileManager* fileManager=[NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
//文件名
NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:filePath];
if ([fileManager fileExistsAtPath:uniquePath]) {
[fileManager removeItemAtPath:uniquePath error:nil];
}
}
再来看Swift获取文件大小,清除缓存,删除沙盒里文件的代码
// 显示缓存大小
func cacheSize() -> CGFloat {
let cachePath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.cachesDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0]
return folderSize(filePath: cachePath)
}
//计算单个文件的大小
func fileSize(filePath: String) -> UInt64 {
let manager = FileManager.default
if manager.fileExists(atPath: filePath) {
do {
let attr = try manager.attributesOfItem(atPath: filePath)
let size = attr[FileAttributeKey.size] as! UInt64
return size
} catch {
print("error :\(error)")
return 0
}
}
return 0
}
//遍历文件夹,返回多少M
func folderSize(filePath: String) -> CGFloat {
let folderPath = filePath as NSString
let manager = FileManager.default
if manager.fileExists(atPath: filePath) {
let childFilesEnumerator = manager.enumerator(atPath: filePath)
var fileName = ""
var folderSize: UInt64 = 0
while childFilesEnumerator?.nextObject() != nil {
fileName = childFilesEnumerator?.nextObject() as! String
let fileAbsolutePath = folderPath.strings(byAppendingPaths: [fileName])
folderSize += fileSize(filePath: fileAbsolutePath[0])
}
return CGFloat(folderSize) / (1024.0 * 1024.0)
}
return 0
}
// 清除缓存
func clearCache() {
let cachPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.cachesDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0] as NSString
let files = FileManager.default.subpaths(atPath: cachPath as String)
for p in files! {
let path = cachPath.appendingPathComponent(p)
if FileManager.default.fileExists(atPath: path) {
do {
try FileManager.default.removeItem(atPath: path)
} catch {
print("error:\(error)")
}
}
}
}
//删除沙盒里的文件
func deleteFile(filePath: String) {
let manager = FileManager.default
let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0] as NSString
let uniquePath = path.appendingPathComponent(filePath)
if manager.fileExists(atPath: uniquePath) {
do {
try FileManager.default.removeItem(atPath: uniquePath)
} catch {
print("error:\(error)")
}
}
}