Java文件写毛刺

时间:2016-09-08 18:00:04

标签: java json

我已经在Java应用程序上工作了几个月而且我刚刚遇到了一个奇怪的错误。

用户数据当前保存到一堆嵌套的HashMaps中,然后保存,然后另一个线程进入,读取哈希映射,将其转换为子,并将其写入文件。我用来将连接写入文件的类使用Google Gson,并且预定义了这个子的格式。该类只是迭代HashMaps并获取值并将其填充到json中。

public static void writeFile(String fileLocation) {
    workingFile = fileLocation;
    long startTime = System.nanoTime();
    JsonWriter writer;
    try {
        File file = new File(fileLocation);
        writer = new JsonWriter(new FileWriter(file.getAbsoluteFile()));

        String[] days = { "saturday", "sunday" };
        String[] times = { "am", "pm" };

        writer.beginObject();
        for (String day : days) {
            writer.name(day);
            writer.beginObject();
            writer.name("date").value(data.get(day).getDate());
            writer.name("races");
            writer.beginObject();
            for (String time : times) {
                writer.name(time);
                writer.beginObject();
                for (Division division : Division.values()) {
                    writer.name(division.getName());
                    writer.beginObject();
                    if (time.equals("am")) {
                        writer.name("elimination").value("double");
                    } else if (time.equals("pm")) {
                        writer.name("elimination").value("single");
                    }
                    writer.name("defined").value(data.get(day).getTime(time).getRace(division).getDefined());
                    writer.name("racers").value(data.get(day).getTime(time).getRace(division).getRacers());
                    writer.name("heats");
                    writer.beginArray();
                    if (data.get(day).getTime(time).getRace(division).getDefined()) {
                        for (Entry<Integer, Heat> heat : data.get(day).getTime(time).getRace(division)
                                .getSheet().heats.entrySet()) {

                            String driver_1 = heat.getValue().getDriver1();
                            String time_1a = heat.getValue().getDriver1PhaseAString();
                            String time_1b = heat.getValue().getDriver1PhaseBString();
                            String driver_2 = heat.getValue().getDriver2();
                            String time_2a = heat.getValue().getDriver2PhaseAString();
                            String time_2b = heat.getValue().getDriver2PhaseBString();
                            String differential = heat.getValue().getDifferentialString();
                            String winner = heat.getValue().getWinner();
                            String loser = heat.getValue().getLoser();

                            writer.beginObject();
                            writer.name("heat").value(heat.getValue().getHeatNumber());
                            writer.name("driver_1").value(driver_1);
                            writer.name("time_1a").value(time_1a);
                            writer.name("time_1b").value(time_1b);
                            writer.name("driver_2").value(driver_2);
                            writer.name("time_2a").value(time_2a);
                            writer.name("time_2b").value(time_2b);
                            writer.name("differential").value(differential);
                            writer.name("winner").value(winner);
                            writer.name("loser").value(loser);
                            writer.name("progress").value(heat.getValue().getProgress());
                            writer.name("force_completed").value(heat.getValue().getForceCompleted());
                            writer.name("drivers_paired").value(heat.getValue().driversPaired());
                            writer.endObject();

                        }
                    }
                    writer.endArray();
                    writer.name("results");
                    writer.beginObject();
                    if (data.get(day).getTime(time).getRace(division).getDefined()) {
                        for (Entry<Integer, ResultField> resultField : data.get(day).getTime(time).getRace(division)
                                .getSheet().resultFields.entrySet()) {
                            writer.name(resultField.getKey().toString()).value(resultField.getValue().getText());
                        }
                    }
                    writer.endObject();
                    writer.name("failed uploads");
                    writer.beginArray();
                    if (data.get(day).getTime(time).getRace(division).getDefined()) {
                        for (int heat : data.get(day).getTime(time).getRace(division).getSheet().failed_uploads) {
                            writer.value(heat);
                        }
                    }
                    writer.endArray();
                    writer.endObject();
                }
                writer.endObject();
            }
            writer.endObject();
            writer.endObject();
        }
        writer.endObject();

        writer.close();
        long endTime = System.nanoTime();
        System.out.println("Done! File written in " + (endTime - startTime) / 1000000 + " ms.");

    } catch (IOException e) {
        e.printStackTrace();
    }

}

今天发生了一件奇怪的事情,这当然是在我演示时发生的。我通过程序的用户界面进行了更改,看起来数据已保存。当我完全退出应用程序时,该文件中没有要读取的数据。是的,我检查过我已将其保存到正确的文件中。实际的json格式是存在的,除了我的值应该没有的地方。

好像文件正在写,它试图读取我的变量,但它们被删除了。我可以在编写文件时退出应用程序,并且它所需的变量不再存在吗?

问题:我是否正确处理保存数据?我无法重现这个错误,这让我很担心,因为我不确定出了什么问题。

0 个答案:

没有答案