iOS 自定义读写文件

2018-02-28 07:52:42来源:cnblogs.com作者:东方🐺人点击

分享
第七城市

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
第七城市

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台