DataOutputStream
is not supposed to write textual files. Instead you use it to convert the standard java primitive type to bytes, therefore writing it compactly.
This results in a binary file, which you are not supposed to read.
If you want to read it, you would have to format the number as text (e.g. Integer.toString
). The disadvantage is that this can require signifantly more space, depending on the value and the base.
Formatted in decimal as 2147483647
it would require 10 byte, whilst in binary it would constantly consume 4 byte. Note that values with only three digits require less space.
You could use the DataOutputStream to write the number in textform:
dos.writeBytes(Integer.toString(yourNumber));
If you really want to write a text file, you should use the BufferedWriter
:
BufferedWriter writer = new BufferedWriter(new FileWriter("out.txt"));
writer.write("Whatever text.");
writer.write(Integer.toString(42));
writer.newLine();
writer.close();
If you wan't to parse it, you probably want to have a look at Scanner
.
However, from guessing what you wan't to do, that might be what you need. I guess you want to store the state of your quiz-like app, in this case the answers.
There are numerous easier ways to achieve that:
- Serialization
- You can use the standard, if it doesn't prove too slow
- Writing the file in binary
- Should be easier because of the fixed length of primitive values, using
DataOutputStream
.
- Should be easier because of the fixed length of primitive values, using