Tuesday, 2017-11-21, 12:09 PM
Welcome Guest | RSS

iPhone全球创业平台

iPhone Global Entrepreneurship Platform

[ New messages · Members · Forum rules · Search · RSS ]
Page 1 of 11
Forum moderator: Freeman 
创业平台论坛 » 开发资源分享 » 源代码分享区 » [文件管理]读写文件代码
[文件管理]读写文件代码
FreemanDate: Saturday, 2011-03-05, 10:00 PM | Message # 1
GROUP: Administrators
Messages: 71
Status: Offline
从应用程序的Documents目录中读取文本文件,写入文本文件用代码。

#pragma mark -
#pragma mark file io method

+ (NSMutableString *)getFullFileName:(NSString *) fileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSString *fileString = [path stringByAppendingPathComponent:fileName];
return [NSMutableString stringWithString: fileString];
}

+ (bool)isFileExists:(NSString *)filepath {
NSFileManager *fileManager = [NSFileManager defaultManager];
return [fileManager fileExistsAtPath:filepath];
}

+ (NSMutableString *)getFileString: (NSString *) fileName {

NSMutableString *dataString;

if ([self isFileExists:[self getFullFileName:fileName]]) {
NSData *data = [[[NSData alloc] initWithContentsOfFile:[self getFullFileName:fileName]] autorelease];
dataString = [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
else {
dataString = [NSMutableString stringWithString:@""];
}

return dataString;
}

+ (bool)setFileString:(NSMutableString *) strSaving fileNameString:(NSString *) fileName {
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:strSaving];
return ([data writeToFile:[self getFullFileName:fileName] atomically:YES]);
}

+ (NSData *)getFileData: (NSString *) fileName {

NSData *dataSaved;

if ([self isFileExists:[self getFullFileName:fileName]]) {
NSData *data = [[[NSData alloc] initWithContentsOfFile:[self getFullFileName:fileName]] autorelease];
dataSaved = [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
else {
dataSaved = [NSData data];
}

return dataSaved;
}

+ (bool)setFileData:(NSObject *) dateSaving fileName:(NSString *) fileName {
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:dateSaving];
return ([data writeToFile:[self getFullFileName:fileName] atomically:YES]);
}

 
skDate: Sunday, 2011-03-06, 1:56 PM | Message # 2
GROUP: Checked
Messages: 4
Status: Offline
这个是干什么用的
 
FreemanDate: Sunday, 2011-03-06, 11:51 PM | Message # 3
GROUP: Administrators
Messages: 71
Status: Offline
加了代码介绍
 
创业平台论坛 » 开发资源分享 » 源代码分享区 » [文件管理]读写文件代码
Page 1 of 11
Search: