我正在尝试以如下特定格式写入文件Days2.dat :
Sunday
Advanced Open Water Dive Certificate
Sailing
Generation Next
Monday
Helping Hands
Beach Touch Rugby
Running Club
Yoga Club
Tennis Lessons
Tuesday
Recycling Club
Best Buddies
Crochet Club
Movie Club
Shooting Club
Wednesday
Table Tennis
Modern Dance
Contemporary Dance
Gavel Club
Thursday
Cooking
Gym Training
单击按钮时,执行的操作应:
从文件Activities.dat中读取并确定活动的日期和名称
以上述格式写入Days2.dat文件, Activities.dat格式如下:
天
活动名称
主管姓名
地点
有薪酬的?
例如
Sunday
Advanced Open Water Dive Certificate
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
Monday
Beach Touch Rugby
Mr.Jones
N/A
No
当前要写入Days2.dat的代码
File f1 = new File("Days2.dat");
File f2 = new File("Activities.dat");
f1.delete();
try{
BufferedWriter writer = new BufferedWriter(new FileWriter(f1, true));
BufferedReader reader = new BufferedReader(new FileReader(f2));
while(reader.ready())
{
String CDay = reader.readLine();
String CActivityName = reader.readLine();
String CSupervisor = reader.readLine();
String CLocation = reader.readLine();
String CPaid = reader.readLine();
String nothing = reader.readLine();
writer.write("Sunday" + "\n");
if(CDay.equals("Sunday"))
{
writer.write(CActivityName + "\n");
}
writer.write("Monday" + "\n");
if(CDay.equals("Monday"))
{
writer.newLine();
writer.write(CActivityName + "\n");
}
writer.write("Tuesday" + "\n");
if(CDay.equals("Tuesday"))
{
writer.newLine();
writer.write(CActivityName + "\n");
}
writer.write("Wednesday" + "\n");
if(CDay.equals("Wednesday"))
{
writer.newLine();
writer.write(CActivityName + "\n");
}
writer.write("Thursday" + "\n");
if(CDay.equals("Thursday"))
{
writer.newLine();
writer.write(CActivityName + "\n");
}
}
writer.flush();
writer.close();
reader.close();
}
catch (IOException ex) {
Logger.getLogger(StartUpFrame.class.getName()).log(Level.SEVERE, null, ex);
}
然而,当前的输出是这样的(有点长,抱歉) - 错误很明显:
Sunday
Advanced Open Water Dive Certificate
Monday
Tuesday
Wednesday
Thursday
Sunday
Sailing
Monday
Tuesday
Wednesday
Thursday
Sunday
Generation Next
Monday
Tuesday
Wednesday
Thursday
Sunday
Monday
Helping Hands
Tuesday
Wednesday
Thursday
Sunday
Monday
Beach Touch Rugby
Tuesday
Wednesday
Thursday
Sunday
Monday
Running Club
Tuesday
Wednesday
Thursday
Sunday
Monday
Yoga Club
Tuesday
Wednesday
Thursday
Sunday
Monday
Tennis Lessons
Tuesday
Wednesday
Thursday
Sunday
Monday
Tuesday
Recycling Club
Wednesday
Thursday
Sunday
Monday
Tuesday
Best Buddies
Wednesday
Thursday
Sunday
Monday
Tuesday
Crochet Club
Wednesday
Thursday
Sunday
Monday
Tuesday
Movie Club
Wednesday
Thursday
Sunday
Monday
Tuesday
Shooting Club
Wednesday
Thursday
Sunday
Monday
Tuesday
Wednesday
Table Tennis
Thursday
Sunday
Monday
Tuesday
Wednesday
Modern Dance
Thursday
Sunday
Monday
Tuesday
Wednesday
Contemporary Dance
Thursday
Sunday
Monday
Tuesday
Wednesday
Gavel Club
Thursday
Sunday
Monday
Tuesday
Wednesday
Thursday
Cooking
Sunday
Monday
Tuesday
Wednesday
Thursday
Gym Training