Solution 1 :
No, it’s not a typo.
BufferedReader
read text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. Then, it uses as delimiter the common System.lineSeparator()
to split the text values.
Check the Javadoc by yourself.
Problem :
I’ve been reading the book Beginning Android Games and I came across this code and text:
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
I am very confused as it says it writes to a new line(in the save method) so that in the load method, which uses readLine() works properly. However, they are only using write() with no /n characters. How will this work? Is it simply a typo?
Comments
Comment posted by ComputerCraft32
The delimiter is carriage return, correct? When write(string) is called, there are no spaces made from what I can tell. I tried this code in eclipse and looking at the file evertyhing is bunched together. (I change the code a bit) System.out.println’s for the boolean did not work after the save(), probably because it was the value true, along with numbers written next to it (true123). The write method just seems not to be writing the text with spaces or returns
Comment posted by Snix
Exactly, you have to modified the write method in order to make BufferedReader to understand when a piece of property ends. So, simply add a System.lineSeparator() near each value.
Comment posted by ComputerCraft32
Doesn’t that mean that the current code is typo (incorrect)?
Comment posted by Snix
The current code is a typo, the first sentence is not a typo. The BufferedReader needs the separator.
Comment posted by ComputerCraft32
Hmm okay. I find it strange that this code was able to make its way through this book. It’s even in the newer editions.