以特定格式写入文件时出现问题



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

一天

ActivityName

主管的名字

位置

支付吗?

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

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

由于在您的文件中有多个星期,我将尝试如下操作:

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

相关内容

  • 没有找到相关文章

最新更新