java bufferedReader,写入的内容与读取的内容不同

发布于 2024-08-04 12:02:30 字数 410 浏览 11 评论 0原文

我用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贪了杯 2024-08-11 12:02:30

BufferedWriter 和 BufferedReader 与编码无关——它们从不处理实际的编码,因为它们只是缓冲现有的读取器和写入器。

现在,FileWriterFileReader 使用默认系统编码 (urgh)。要解决此问题,您通常应该使用 InputStream / InputStreamReaderOutputStream / OutputStreamWriter 对(可能包含在a BufferedReader / BufferedWriter),并显式指定编码。

你还没有说你实际上在读什么——它是一个文件吗?你知道文件的编码吗?

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?

雨巷深深 2024-08-11 12:02:30

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文