IOS custom read/write files, ios custom read/write
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 // Write File + (BOOL) writeFileName :( NSString *) fileName data :( id) result; // Read File + (id) readFileName :( NSString *) fileName; // calculate the file size + (NSString *) folderSizeStringAtPath :( NSString *) folderPath; // format size to M, K mode + (NSString *) sizeStringFromSizeLong :( long) folderSize; // obtain the file size in directory B + (long) folderSizeAtPath :( NSString *) folderPath; // determine whether the file exists + (BOOL) isExistsFile :( NSString *) filepath; // Delete the cache file + (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) [[using (NSLibraryDirectory, NSUserDomainMask, YES) lastObject] Using: name] @ implementation LSCacheFile + (NSString *) filePath :( NSString *) fileName {NSStri Ng * floderPath = PATH_AT_LIBDIR (@ "Caches/LSCacheFinder"); if (! [[NSFileManager defaultManager] fileExistsAtPath: floderPath isDirectory: nil]) {[[NSFileManager defaultManager] createDirectoryAtPath: floderPath attributes: NO attributes: nil error: nil];} return [floderPath attributes: 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 defaultMa Nager] 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 defamanager manager] removeItemAtP Ath: filepath error: nil] ;}// calculate the total file size in the folder + (NSString *) folderSizeStringAtPath :( NSString *) folderPath {long folderSize = [LSCacheFile folderSizeAtPath: folderPath]; return [self sizeStringFromSizeLong: folderSize];} + (NSString *) sizeStringFromSizeLong :( long) folderSize {if (folderSize <1024) {return @ "0 K ";} else if (folderSize/1024.0 <1024) {return [NSString stringWithFormat: @ "%. 2fK ", folder Size/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) folderSizeAtPath :( NSString *) folderPath {NSFileManager * manager = [NSFileManager defamanager]; if (! [Manager fileExistsAtPath: folderPath]) return 0; NSEnumerator * childFilesEnumerator = [[manager subpathsAtPath: folderPath] objectEnumerator]; NSString * fileName; long folderSize = 0; while (fileName = [childFilesEnumerator nextObject])! = Nil) {NSString * fileAbsolutePath = [folderPath paths: fileName]; folderSize + = [LSCacheFile fileSizeAtPath: fileAbsolutePath];} return folderSize;} + (long) fileSizeAtPath :( NSString *) filePath {struct stat st; if (lstat ([filePath cStringUsingEncoding: NSUTF8StringEncoding], & st) = 0) {return st. st_size;} return 0;} @ end