Issue
If I have a file, and I want to literally write '42' to it (the value, not the string), which for example is 2a in hex, how do I do it? I want to be able to use something like outfile.write(42) or outfile.write(2a) and not write the string to the file.
Solution
For writing binary data you'll want to use a OutputStream
(such as a FileOutputStream
).
If you find that your data is written as strings, then you're probably using a Writer
(such as a FileWriter
or a OutputStreamWriter
wrapped around a FileOutputStream
). Everything named "*Writer
" or "*Reader
" deals exclusively with text/String
s. You'll want to avoid those if you want to write binary data.
If you want to write different data types (and not just plain byte
s), then you'll want to look into the DataOutputStream
.
Answered By - Joachim Sauer
Answer Checked By - Marie Seifert (JavaFixing Admin)