我在閱讀.dat文件時遇到了問題。 目前,我知道如何讀取.dat/.txt文件,但這對我來說有點棘手。在文件中循環,按行設置一組行
所以我有這樣一組數據:
TEST.DAT
Sunday
Scuba Diving Classes
Mr.Jones
N/A
Yes
Sunday
Sailing
Mr. Jackson
N/A
Yes
Sunday
Generation Next
Ms.Steele
N/A
Yes
Monday
Helping Hands
Ms.Wafa
ANX0
No
我需要這個文件做的是去通過記錄文件記錄,如果一個字符串activityname .equals到活動的名稱(例如水肺潛水課程),以C開頭的變量被設置爲查看。下面是它的編碼:
try{
BufferedReader reader = new BufferedReader(new FileReader("test.dat"));
int numberOfLines = readLines();
numberOfLines = numberOfLines/6;
for(int i=0;i < numberOfLines; i++)
{
String CDay = reader.readLine();
String CActivityName = reader.readLine();
String CSupervisor = reader.readLine();
String CLocation = reader.readLine();
String CPaid = reader.readLine();
String nothing = reader.readLine();
if(CActivityName.equals(activityName))
{
txtDay.setText(CDay);
txtSupervisor.setText(CSupervisor);
txtLocation.setText(CLocation);
//If it Activity is paid, then it is shown as paid via radiobutton
if(CPaid.equals("Yes"))
{
radioYes.setSelected(rootPaneCheckingEnabled);
}
if(CPaid.equals("No"))
{
radioNo.setSelected(rootPaneCheckingEnabled);
}
}
else
{
reader.close();
}
}
}
catch(IOException e)
{
Logger.getLogger(AddActivity.class.getName()).log(Level.SEVERE, null, e);
}
}
我使用numberoflines變量要經過該文件,但我的邏輯是有缺陷的,我不知道如何去通過這個
現狀和方法
的誤差目前,這種方法只讀取第一個記錄水肺潛水學校,當如果條件爲假不通過整個文件去。
幫助!
你一旦遇到名稱不等於activityName的活動,請關閉閱讀器。別。 – 2013-03-10 17:50:56
這工作!沒有看到我的邏輯缺陷 謝謝! – Geuni 2013-03-10 17:55:29