ios學習:檔案簡單讀寫

來源:互聯網
上載者:User

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檔案的內容

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.