java bufferedReader, writes something different than it reads
-
07-07-2019 - |
Question
i parsed a text (CAL code) with BufferedReader and BufferedWriter in Java, unfortunately, lines which i red and wrote with outStream.write(line); have changed, please look at Screenshots:
http://uploadz.eu/images/4qz8mtkm2d9zx3x5ms3n.png h**p://uploadz.eu/images/c03hgkrgrmit2ij2mug.png
as you see, some special character did changed the lines although i intended NOT to change them.
as far as i know, Bufferedwriter / Reader should work in unicode by default.
Solution
Well BufferedWriter
and BufferedReader
are encoding agnostic - they never deal with the actual encodings, as they're just buffering existing readers and writers.
Now FileWriter
and FileReader
use the default system encoding (urgh). To work round this, you should usually use an InputStream
/ InputStreamReader
or OutputStream
/ OutputStreamWriter
pair (possibly wrapped in a BufferedReader
/ BufferedWriter
), and specify the encoding explicitly.
You haven't said what you're actually reading from - is it a file? Do you know the encoding of the file?
OTHER TIPS
You should either know the encoding if you're going to treat the content as text (String), or, when copying is your goal, or treat the content as a byte array.