IOS project files are divided into project files and files in the sandbox. The files in the project are read-only and readable and writable. the methods for reading files are the same, whether in the project or in the sandbox. The only difference lies in obtaining the file path.
1. File directory
2. txt files. Fields in each row of data are separated by \ t.
3 twfxcitycode class:
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. header file of the twfxviewcontroller class
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. Read TXT files in the project:
1 nsstring * filepath = [[nsbundle mainbundle] pathforresource: @ "test123" oftype: @ "TXT"]; 2 3 nsarray * filedata; 4 nserror * error; 5 6 // read the file and divide the content according to the linefeed and assign the value to nsarray 7 filedata = [[nsstring stringwithcontentsoffile: filepath 8 encoding: nsutf8stringencoding 9 error: & error] 10 componentsseparatedby: @ "\ n"]; 11 12 13 // gets the nsarray type object iterator 14 nsenumerator * arrayenumerator = [filedata objectenumerator]; 15 nsstring * tempstr; 16 17 while (tempstr = [arrayenumerator nextobject]) {18 19 twfxcitycode * citycode = [[twfxcitycode alloc] init]; 20 21 // separate each line of strings by the \ t character. 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. Save the data to the plist file in the sandbox.
1 // obtain the documents directory of the application sandbox 2 nsarray * paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes); 3 4 nsstring * plistpath1 = [paths objectatindex: 0]; 5 6 // obtain the complete path 7 nsstring * filename = [plistpath1 stringbyappendingpathcomponent: @ "weathertest. plist "]; 8 9 // Save the parsed data on the string. cannot the value in dictionary be a custom object? Or does the custom class need to implement the specified protocol? 10 // No way. temporarily use a dictionary to save value bar 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 file written"); 27 [mydic writetofile: Filename atomically: yes]; 28
7. plist file content in the sandbox