无法以特定格式写入文件

时间:2013-03-10 20:39:42

标签: java file netbeans while-loop bufferedwriter

我正在尝试以这样的特定格式写入 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 文件中读取并识别活动的日期和名称

  • 以上述格式 Activities.dat 格式写入 Days2.dat 文件:

ActivityName

主管姓名

位置

付费?

e.g。

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

1 个答案:

答案 0 :(得分:2)

您目前正在撰写每周的某一天,无论我们实际处理哪一天都不太正确。

由于您的文件中有一周中有多天,我会尝试以下内容:

  • 为一周中的每一天创建List(或其他一些对象)。如果您不想创建如此多的对象,则此处的地图可能会更有用。 Map<String, List<String>>
  • 使用您的CDay变量确定我们正在处理的星期几(正如您目前所做的那样)。
  • 如果匹配,则将活动添加到列表中。
  • 读取文件后,将列表内容写入一周中每一天的新文件中。