java bufferedReader,写入的内容与读取的内容不同
我用 Java 中的 BufferedReader 和 BufferedWriter 解析了文本(CAL 代码),不幸的是,我用 outStream.write(line) 红色并写入了行;已更改,请查看屏幕截图:
http://uploadz.eu/images/4qz8mtkm2d9zx3x5ms3n.png h**p://uploadz.eu/images/c03hgkrgrmit2ij2mug.png
正如你所看到的,一些特殊字符确实改变了这些行,尽管我不想改变它们。
据我所知,Bufferedwriter / Reader 默认应该在 unicode 下工作。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
BufferedWriter 和 BufferedReader 与编码无关——它们从不处理实际的编码,因为它们只是缓冲现有的读取器和写入器。
现在,
FileWriter
和FileReader
使用默认系统编码 (urgh)。要解决此问题,您通常应该使用InputStream
/InputStreamReader
或OutputStream
/OutputStreamWriter
对(可能包含在aBufferedReader
/BufferedWriter
),并显式指定编码。你还没有说你实际上在读什么——它是一个文件吗?你知道文件的编码吗?
Well
BufferedWriter
andBufferedReader
are encoding agnostic - they never deal with the actual encodings, as they're just buffering existing readers and writers.Now
FileWriter
andFileReader
use the default system encoding (urgh). To work round this, you should usually use anInputStream
/InputStreamReader
orOutputStream
/OutputStreamWriter
pair (possibly wrapped in aBufferedReader
/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?
如果您要将内容视为文本(字符串),或者当您的目标是复制时,您应该知道编码,或者将内容视为字节数组。
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.