ios学习:文件简单读写
2013.10.05补充 参考自 http://www.cnblogs.com/chinapcc/archive/2013/04/03/2673092.html
要把自定义类保存到plist文件里,要注意一下三点:
1、自定义类要继承NSCoding协议,实现 -(void)encodeWithCoder:(NSCoder *)coder 方法和 - (id) initWithCoder: (NSCoder *)coder方法
2、在写入plist文件时要对数据序列化然后写入:[NSKeyedArchiver archiveRootObject:array toFile:filename];array是要写入的数据,filename是plist文件路径
3、在读取plist文件数据时要对数据反序列化才能用:NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile: filename];
建议在有条件的情况下还是把数据放到 NSMutableArray / NSArray / NSMutableDictionary / NSDictionary 里,写入plist文件吧
ios项目的文件分工程项目里的文件和程序沙盒里的文件两种,其中项目工程里的文件是只读的,为沙盒里的文件可读可写.对于读文件而言,不管是工程里的还是沙盒的,方法都一样,唯一不同的在于文件路径的获取.
1 文件目录
2 txt文件.每一行数据的字段之间是用 \t 来分割
3 TWFXCityCode类:
1 #import <Foundation/Foundation.h> 2 3 @interface TWFXCityCode : NSObject 4 { 5 @public 6 NSString *cityName; 7 NSString *cityCode; 8 NSString *pinyin; 9 NSString *sortPinyin; 10 NSString *provinceName; 11 NSString *provinceCode; 12 } 13 14 @end
4 TWFXViewController类的头文件
1 #import <UIKit/UIKit.h> 2 #import "TWFXCityCode.h" 3 4 @interface TWFXViewController : UIViewController 5 6 @property (retain, nonatomic) NSMutableArray *weatherData; 7 8 - (IBAction)btnClick_readFile:(UIButton *)sender; 9 - (IBAction)btnClick_writePlist:(UIButton *)sender; 10 - (IBAction)btnClick_readPlist:(UIButton *)sender; 11 - (IBAction)btnClick_test:(UIButton *)sender; 12 @end
5 项目工程里txt文件的读取:
1 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test123" ofType:@"txt"]; 2 3 NSArray *fileData; 4 NSError *error; 5 6 //读取file文件并把内容根据换行符分割后赋值给NSArray 7 fileData = [[NSString stringWithContentsOfFile:filePath 8 encoding:NSUTF8StringEncoding 9 error:&error] 10 componentsSeparatedByString:@"\n"]; 11 12 13 //获取NSArray类型对象的迭代器 14 NSEnumerator *arrayEnumerator = [fileData objectEnumerator]; 15 NSString *tempStr; 16 17 while (tempStr = [arrayEnumerator nextObject]) { 18 19 TWFXCityCode *cityCode = [[TWFXCityCode alloc] init]; 20 21 //把每行字符串再按 \t 符分割 22 NSArray *cityInfo = [tempStr componentsSeparatedByString:@"\t"]; 23 24 cityCode->cityCode = cityInfo[0]; 25 cityCode->cityName = cityInfo[1]; 26 cityCode->pinyin = cityInfo[2]; 27 cityCode->sortPinyin = cityInfo[3]; 28 cityCode->provinceCode = cityInfo[4]; 29 cityCode->provinceName = cityInfo[5]; 30 31 [self.weatherData addObject:cityCode]; 32 [cityCode release]; 33 }
6 把数据保存到沙盒里的plist文件
1 //获取应用程序沙盒的Documents目录 2 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 3 4 NSString *plistPath1 = [paths objectAtIndex:0]; 5 6 //得到完整路径名 7 NSString *filename = [plistPath1 stringByAppendingPathComponent:@"weathertest.plist"]; 8 9 //保存对字符串解析后的数据.dictionary里的value不能是自定类型对象么?还是说自定义类要实现指定的协议? 10 //没办法,暂时先用一个dictionary来保存value吧 11 NSMutableDictionary *myDic = [[NSMutableDictionary alloc] init]; 12 13 for (TWFXCityCode *city in self.weatherData) { 14 15 NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] init]; 16 [tempDic setValue:city->cityCode forKey:@"cityID"]; 17 [tempDic setValue:city->cityName forKey:@"cityName"]; 18 [tempDic setValue:city->pinyin forKey:@"cityPinyin"]; 19 [tempDic setValue:city->sortPinyin forKey:@"citySortPinyin"]; 20 [tempDic setValue:city->provinceCode forKey:@"provinceID"]; 21 [tempDic setValue:city->provinceName forKey:@"provinceName"]; 22 23 [myDic setValue:tempDic forKey:city->cityCode]; 24 } 25 26 NSLog(@"写plist文件完成"); 27 [myDic writeToFile:filename atomically:YES]; 28
7 沙盒里plist文件的内容