0

我正在尝试以如下特定格式写入文件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
4

1 回答 1

2

无论我们实际处理哪一天,您当前每次都在编写星期几,这并不完全正确。

由于您的文件中有一周中的多天,我会尝试如下操作:

  • 为一周中的每一天创建一个List(或其他对象)。如果您不想创建这么多对象,则地图可能在这里更有用,例如Map<String, List<String>>
  • 使用您的 CDay 变量确定我们正在处理一周中的哪一天(就像您当前所做的那样)。
  • 如果匹配,则将活动添加到列表中。
  • 读取文件后,将列表的内容写入一周中每一天的新文件中。
于 2013-03-10T20:54:27.317 回答