有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java这只是一个打字错误吗?

我一直在读《Android游戏入门》一书,我遇到了以下代码和文本:

public static void load(FileIO files) {
    BufferedReader in = null;
    try { in = new BufferedReader(new InputStreamReader(
            files.readFile(".mrnom")));
        soundEnabled = Boolean.parseBoolean( in .readLine());
        for (int i = 0; i < 5; i++) {
            highscores[i] = Integer.parseInt( in .readLine());
        }
    } catch (IOException e) {
        // :( It's ok we have defaults
    } catch (NumberFormatException e) {
        // :/ It's ok, defaults save our day
    } finally {
        try {
            if ( in != null)
                in .close();
        } catch (IOException e) {}
    }
}
public static void save(FileIO files) {
    BufferedWriter out = null;
    try {
        out = new BufferedWriter(new OutputStreamWriter(
            files.writeFile(".mrnom")));
        out.write(Boolean.toString(soundEnabled));
        for (int i = 0; i < 5; i++) {
            out.write(Integer.toString(highscores[i]));
        }
    } catch (IOException e) {} finally {
        try {
            if (out != null)
                out.close();
        } catch (IOException e) {}
    }
}

Next up is a method called save(). It takes the current settings and serializes them to the .mrnom file on the external storage (e.g., /sdcard/.mrnom). The sound setting and each high-score entry is stored as a separate line in that file, as expected by the load() method. If something goes wrong, we just ignore the failure and use the default values defined earlier. In an AAA title, you might want to inform the user about this loading error

我很困惑,因为它说它会写入一个新行(在save方法中),所以在load方法中,使用readLine()可以正常工作。但是,它们只使用不带/n字符的write()。这将如何工作?这只是一个打字错误吗


共 (1) 个答案

  1. # 1 楼答案

    不,这不是打字错误
    BufferedReader从字符输入流读取文本,缓冲字符,以便有效读取字符、数组和行。然后,它使用公共System.lineSeparator()作为分隔符来分割文本值。 自己检查一下Javadoc