iOS 自訂讀寫檔案,ios自訂讀寫
LSCacheFile.h
//// LSCacheFile.h// iPhone//// Created by xujinzhong on 14-6-5.// Copyright (c) 2014年 xujinzhong. All rights reserved.//#import <Foundation/Foundation.h>#import "LSCommonMacro.h"@interface LSCacheFile : NSObject//寫入檔案+ (BOOL)writeFileName:(NSString *)fileName data:(id)result;//讀出檔案+ (id)readFileName:(NSString *)fileName;//計算檔案大小+ (NSString *)folderSizeStringAtPath:(NSString *)folderPath;//格式化size為M,K模式+(NSString*)sizeStringFromSizeLong:(long long) folderSize;//以B方式擷取目錄下的檔案大小+ (long long)folderSizeAtPath:(NSString*)folderPath;//判斷檔案是否存在+ (BOOL)isExistsFile:(NSString *)filepath;//刪除快取檔案+ (void)deleteCacheFile:(NSString *)filepath;@end
LSCacheFile.m
//// LSCacheFile.m// iPhone//// Created by xujinzhong on 14-6-5.// Copyright (c) 2014年 xujinzhong. All rights reserved.//#import "LSCacheFile.h"#include "sys/stat.h"#define PATH_AT_LIBDIR(name) [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:name]@implementation LSCacheFile+ (NSString *)filePath:(NSString *)fileName{ NSString *floderPath = PATH_AT_LIBDIR(@"Caches/LSCacheFinder"); if(![[NSFileManager defaultManager] fileExistsAtPath:floderPath isDirectory:nil]){ [[NSFileManager defaultManager] createDirectoryAtPath:floderPath withIntermediateDirectories:NO attributes:nil error:nil]; } return [floderPath stringByAppendingPathComponent:fileName];}+ (BOOL)writeFile:(NSString *)fileName object:(id)object{ if(fileName == nil){ return NO; } if(![NSKeyedArchiver archiveRootObject:object toFile:fileName]){ return NO; } return YES;}+ (id)readFile:(NSString *)fileName{ if(fileName == nil){ return nil; } return [NSKeyedUnarchiver unarchiveObjectWithFile:fileName];}+ (BOOL)writeFileName:(NSString *)fileName data:(id)result{ NSString *pathName = [LSCacheFile filePath:fileName]; if ([LSCacheFile isExistsFile:pathName]) { [[NSFileManager defaultManager] removeItemAtPath:pathName error:nil]; } return [LSCacheFile writeFile:pathName object:result];}+ (id)readFileName:(NSString *)fileName{ if (!fileName) { return nil; } if(![LSCacheFile isExistsFile:fileName]){ return nil; } id object = [LSCacheFile readFile:[LSCacheFile filePath:fileName]]; if (!object) { return nil; } return object;}+ (BOOL)isExistsFile:(NSString *)filepath{ NSFileManager *filemanage = [NSFileManager defaultManager]; return [filemanage fileExistsAtPath:[LSCacheFile filePath:filepath]];}+(void)deleteCacheFile:(NSString *)filepath{ BOOL flag = NO; if([[NSFileManager defaultManager] fileExistsAtPath:filepath isDirectory:&flag]) { [[NSFileManager defaultManager] removeItemAtPath:filepath error:nil]; }}//計算檔案夾下檔案的總大小+ (NSString *)folderSizeStringAtPath:(NSString *)folderPath{ long long folderSize = [LSCacheFile folderSizeAtPath:folderPath]; return [self sizeStringFromSizeLong:folderSize];}+(NSString*)sizeStringFromSizeLong:(long long) folderSize{ if (folderSize < 1024) { return @"0K"; }else if(folderSize/1024.0 < 1024){ return [NSString stringWithFormat:@"%.2fK",folderSize/1024.0]; }else if(folderSize/1024.0/1024.0 < 1024){ return [NSString stringWithFormat:@"%.2fM",folderSize/1024.0/1024.0]; }else if(folderSize/1024.0/1024.0/1024.0 < 1024){ return [NSString stringWithFormat:@"%.2fG",folderSize/1024.0/1024.0/1024.0]; } return @"";}+ (long long)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 += [LSCacheFile fileSizeAtPath:fileAbsolutePath]; } return folderSize;}+ (long long)fileSizeAtPath:(NSString*)filePath{ struct stat st; if(lstat([filePath cStringUsingEncoding:NSUTF8StringEncoding], &st) == 0){ return st.st_size; } return 0;}@end