简体   繁体   中英

java bufferedReader, writes something different than it reads

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.

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?

如果要将内容视为文本(字符串),或者应该以复制为目标,则应该知道编码,或者将内容视为字节数组。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM