Iphone文件操作和字符串操作的工具类
这个工具类没必要多解释,直接上代码:
.h文件:
.m文件:
转载自:http://lib.open-open.com/view/open1335678396202.html
.h文件:
#import <Foundation/Foundation.h> #define DEFAULT_DATE_TIME_FORMAT (@"yyyy-MM-dd'T'HH:mm:ss'Z'") #define DEFAULT_DATE_FORMAT (@"yyyy-MM-dd") #define DEFAULT_TIME_FORMAT (@"HH:mm:ss'Z'") @interface FileUtils : NSObject { } +(NSString*) settingPath; +(NSString*) cachePath; +(BOOL) clearCache:(NSError**)error; +(NSString*) fileAtBundleResource:(NSString*)file; +(BOOL) fileExist:(NSString*)file; +(BOOL) fileExistAtCache:(NSString*)file; +(NSString*) cachedFile:(NSString*)file; +(BOOL) writeToFile:(NSString*)fileWithPath withData:(NSData*)data; +(BOOL)deleteFile:(NSString*)fileWithPath; +(NSString*) documentPathWith:(NSString*)cmp; @end @interface ValueUtils : NSObject { } +(BOOL) isEmpty:(NSString*)text; +(BOOL) boolValue:(NSDictionary*)dict forKey:(id)key; +(float) floatValue:(NSDictionary*)dict forKey:(id)key; +(NSInteger) intValue:(NSDictionary*)dict forKey:(id)key; +(NSString*) stringValue:(NSDictionary*)dict forKey:(id)key; +(NSArray*) arrayValue:(NSDictionary*)dict forKey:(id)key; +(id) noneNullValue:(NSDictionary*)dict forKey:(id)key; +(NSDictionary*) dictionaryValue:(NSDictionary*)dict forKey:(id)key; +(BOOL) stringEquals:(NSString*)str1 to:(NSString*)str2; +(BOOL) caseEquals:(NSString*)str1 to:(NSString*)str2; +(NSString*) refineUrl:(NSString*)url; +(BOOL) isURLString:(NSString*)text; +(BOOL) startWith:(NSString*)prefix forString:(NSString*)text; +(BOOL) endWith:(NSString*)suffix forString:(NSString*)text; +(NSDate *)stringToDate:(NSString *)string withFormat:(NSString*)fmt; +(NSString*) dateToString:(NSDate*)date withFormat:(NSString*)fmt; +(NSString*) idToString:(id)obj; +(BOOL) booleanToBool:(id)bobj; @end
.m文件:
#import "FileUtils.h" #import "CJSONDeserializer.h" #import "CJSONSerializer.h" @implementation FileUtils +(NSString*) settingPath { ////Make a new director NSFileManager *fileManager = [NSFileManager defaultManager]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString *documentsDirectory = [paths objectAtIndex:0]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@".settings"]; BOOL folder = NO; BOOL exist = [fileManager fileExistsAtPath:documentsDirectory isDirectory:&folder]; if ( exist == YES && folder == YES ) { return [documentsDirectory stringByAppendingPathComponent:@"measets.ini"]; }else{ NSError* error = nil; [fileManager createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:&error]; if( error != nil ){ NSLog(@"Can not create directory %@. Error is %@.", documentsDirectory, [error description]); return nil; } return documentsDirectory; } /*** NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectoryPath = [paths objectAtIndex:0]; NSString *secondaryDirectoryPath = [documentsDirectoryPath stringByAppendingPathComponent:@"secondary"]; NSString *databaseFile = [secondaryDirectoryPath stringByAppendingPathComponent:@"database.db"]; NSFileManager *fileManager = [NSFileManager defaultManager]; [fileManager removeItemAtPath:databaseFile error:NULL]; ***/ } +(BOOL) fileExist:(NSString*)file { NSFileManager *fileManager = [NSFileManager defaultManager]; return [fileManager fileExistsAtPath:file]; } +(NSString*) fileAtBundleResource:(NSString*)file { return [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:file]; } +(BOOL) fileExistAtCache:(NSString*)file { return [self fileExist:[self cachedFile:file]]; } +(NSString*) cachedFile:(NSString*)file { NSString* fileAtCache = [NSString stringWithFormat:@"%@%@",[self cachePath], file]; return fileAtCache; } +(BOOL) writeToFile:(NSString*)fileWithPath withData:(NSData*)data { BOOL folder = NO; NSFileManager *fileManager = [NSFileManager defaultManager]; NSString* fileName = [fileWithPath lastPathComponent]; NSInteger toIndex = [fileWithPath length] - [fileName length]; NSString* path = [fileWithPath substringToIndex:toIndex]; BOOL exist = [fileManager fileExistsAtPath:path isDirectory:&folder]; if ( exist != YES ) { NSError* error = nil; [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error]; if( error != nil ){ NSLog(@"Can not create directory %@. Error is %@.", path, [error description]); return NO; } } if ( data != nil ) { return [data writeToFile:fileWithPath atomically:YES]; } return NO; } +(NSString*) documentPathWith:(NSString*)cmp { ////Make a new director NSFileManager *fileManager = [NSFileManager defaultManager]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString *documentsDirectory = [paths objectAtIndex:0]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent:cmp]; BOOL folder = NO; BOOL exist = [fileManager fileExistsAtPath:documentsDirectory isDirectory:&folder]; if ( exist == YES && folder == YES ) { return documentsDirectory; }else{ NSError* error = nil; [fileManager createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:&error]; if( error != nil ){ NSLog(@"Can not create directory %@. Error is %@.", documentsDirectory, [error description]); return nil; } return documentsDirectory; } } +(NSString*) cachePath { ////Make a new director NSFileManager *fileManager = [NSFileManager defaultManager]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString *documentsDirectory = [paths objectAtIndex:0]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@".caches"]; BOOL folder = NO; BOOL exist = [fileManager fileExistsAtPath:documentsDirectory isDirectory:&folder]; if ( exist == YES && folder == YES ) { return documentsDirectory; }else{ NSError* error = nil; [fileManager createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:&error]; if( error != nil ){ NSLog(@"Can not create directory %@. Error is %@.", documentsDirectory, [error description]); return nil; } return [documentsDirectory stringByAppendingPathComponent:@"measets.ini"]; } } +(BOOL)deleteFile:(NSString*)fileWithPath { NSFileManager *fileManager = [NSFileManager defaultManager]; NSError* error = nil; BOOL ret = [fileManager removeItemAtPath:fileWithPath error:&error]; if ( error != nil ) { NSLog(@"DeleteFile with an error: %@.", [error description]); } return ret; } +(BOOL) clearCache:(NSError**)error { NSFileManager *fileManager = [NSFileManager defaultManager]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString *documentsDirectory = [paths objectAtIndex:0]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@".caches"]; [fileManager removeItemAtPath:documentsDirectory error:error]; return YES; } @end @implementation ValueUtils +(BOOL) isEmpty:(NSString*)text { if (text == nil) { return YES; } if ([text length] == 0 ) { return YES; } return NO; } +(NSString*) idToString:(id)obj { if ( obj == nil ) { return nil; } if ( [obj isKindOfClass:[NSNull class]]) { return nil; } if ( [obj isKindOfClass:[NSString class]]) { return (NSString*)obj; }else if ( [obj isKindOfClass:[NSArray class]] ){ NSMutableString* ntf = [NSMutableString string]; for (id t in obj ) { [ntf appendString:[self idToString:t]]; [ntf appendString:@","]; } return ntf; }else { if( CFGetTypeID(obj) == CFBooleanGetTypeID() ){ return [NSString stringWithFormat:@"%@", (CFBooleanGetValue((CFBooleanRef)obj) == true ? @"true" : @"false") ]; } if ( sizeof(obj) == sizeof(BOOL) ) { //return (BOOL)obj == YES ? @"true" : @"false"; } if( [obj respondsToSelector:@selector(stringValue)] ){ return [obj stringValue]; } return [obj description]; } } +(BOOL) boolValue:(NSDictionary*)dict forKey:(id)key { id bl = [dict objectForKey:key]; if ( bl == nil || [bl isKindOfClass:[NSNull class]]) { return NO; } if ( [bl isKindOfClass:[NSString class]]) { NSString* cstr = [bl lowercaseString]; if ( [cstr isEqualToString:@"yes"] || [cstr isEqualToString:@"true"]) { return YES; } return [cstr boolValue]; } if ( CFBooleanGetValue((CFBooleanRef)bl) ) { return YES; } return NO; } +(float) floatValue:(NSDictionary*)dict forKey:(id)key{ id bl = [dict objectForKey:key]; if ( bl == nil || [bl isKindOfClass:[NSNull class]]) { return 0.0f; } return [bl floatValue]; } +(NSInteger) intValue:(NSDictionary*)dict forKey:(id)key { id bl = [dict objectForKey:key]; if ( bl == nil || [bl isKindOfClass:[NSNull class]]) { return 0.0f; } return [bl floatValue]; } +(NSString*) stringValue:(NSDictionary*)dict forKey:(id)key { id bl = [dict objectForKey:key]; if ( bl == nil || [bl isKindOfClass:[NSNull class]]) { return nil; } return [self idToString:bl]; } +(NSArray*) arrayValue:(NSDictionary*)dict forKey:(id)key { id bl = [dict objectForKey:key]; if ( bl == nil || [bl isKindOfClass:[NSNull class]]) { return nil; } if ( [bl isKindOfClass:[NSArray class]]) { return (NSArray*)bl; } return nil; } +(NSDictionary*) dictionaryValue:(NSDictionary*)dict forKey:(id)key { id bl = [dict objectForKey:key]; if ( bl == nil || [bl isKindOfClass:[NSNull class]]) { return nil; } return (NSDictionary*)bl; } +(id) noneNullValue:(NSDictionary*)dict forKey:(id)key { id bl = [dict objectForKey:key]; if ( bl == nil || [bl isKindOfClass:[NSNull class]]) { return nil; } return bl; } +(BOOL) stringEquals:(NSString*)str1 to:(NSString*)str2 { if ( str1 == nil || str2 == nil ) { return NO; } return [str1 compare:str2 options:NSCaseInsensitiveSearch] == NSOrderedSame; } +(BOOL) caseEquals:(NSString*)str1 to:(NSString*)str2 { return (str1 == nil || str2 == nil) ? NO : [str1 isEqualToString:str2]; } +(BOOL) startWith:(NSString*)prefix forString:(NSString*)text; { if ( text != nil && prefix != nil ){ if ( [prefix length] > [text length] ) { return NO; } NSString* prestr = [text substringToIndex:[prefix length]]; if ( [self stringEquals:prestr to:prefix] ) { return YES; } } return NO; } +(BOOL) endWith:(NSString*)suffix forString:(NSString*)text { if ( text != nil && suffix != nil ){ if ( [suffix length] > [text length] ) { return NO; } NSInteger len = [text length] - [suffix length]; NSString* sufstr = [text substringFromIndex:len]; if ( [self caseEquals:sufstr to:suffix] ) { return YES; } } return NO; } +(BOOL) isURLString:(NSString*)text { if ( [text length] > 6 ) { NSString* prefix = [text substringToIndex:6]; if ( [self stringEquals:prefix to:@"http:/"] || [self stringEquals:prefix to:@"https:"] ) { return YES; } else if ( [self stringEquals:prefix to:@"local:" ] ){ return YES; } } if ( [self startWith:@"/" forString:text] ){ return YES; } return NO; } +(NSString*) refineUrl:(NSString*)url { return url; } +(BOOL) booleanToBool:(id)bobj { if ( bobj == nil ) { return NO; } if ( [bobj isKindOfClass:[NSString class]] ) { return [bobj caseInsensitiveCompare:@"yes"] == 0 || [bobj caseInsensitiveCompare:@"true"] == 0; }else if ( [bobj respondsToSelector:@selector(boolValue)] ){ return [bobj boolValue]; }else { return CFBooleanGetValue((CFBooleanRef)bobj); } return NO; } +(NSDate *)stringToDate:(NSString *)string withFormat:(NSString*)fmt{ NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setTimeZone:[NSTimeZone defaultTimeZone]]; //#define kDEFAULT_DATE_TIME_FORMAT (@"yyyy-MM-dd'T'HH:mm:ss'Z'") NSString* format = fmt == nil ? DEFAULT_DATE_TIME_FORMAT : fmt; [formatter setDateFormat:format]; NSDate *date = [formatter dateFromString:string]; [formatter release]; return date; } +(NSString*) dateToString:(NSDate*)date withFormat:(NSString*)fmt { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setTimeZone:[NSTimeZone defaultTimeZone]]; //#define kDEFAULT_DATE_TIME_FORMAT (@"yyyy-MM-dd'T'HH:mm:ss'Z'") NSString* format = fmt == nil ? DEFAULT_DATE_FORMAT : fmt; [formatter setDateFormat:format]; NSString* dateStr = [formatter stringFromDate:date]; [formatter release]; return dateStr; } @end
转载自:http://lib.open-open.com/view/open1335678396202.html