iOS 自訂讀寫檔案,ios自訂讀寫

來源:互聯網
上載者:User

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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.