Java 7:调用 Files.newBufferedReader 时应使用什么字符集?
在以前版本的 Java 中,我会通过创建缓冲读取器来读取文件,如下所示:
BufferedReader in = new BufferedReader(new FileReader("file.txt"));
在 Java 7 中,我想使用 Files.newBufferedReader
,但我还需要传入一个字符集。例如:
BufferedReader in = Files.newBufferedReader(Paths.get("file.txt"),
Charset.forName("US-ASCII"));
以前,我在读取纯文本文件时不必担心字符集。我应该使用什么字符集?你知道以前版本的 Java 默认使用什么字符集吗?我只是希望能够找到旧语句并将其替换为新语句。
In previous versions of Java, I would read a file by creating a buffered reader like this:
BufferedReader in = new BufferedReader(new FileReader("file.txt"));
In Java 7, I would like to use Files.newBufferedReader
, but I need to pass in a charset as well. For example:
BufferedReader in = Files.newBufferedReader(Paths.get("file.txt"),
Charset.forName("US-ASCII"));
Previously, I did not have to worry about charsets when reading plain text files. What charset shall I use? Do you know what charset was used by default in previous versions of Java? I simply want to be able to find and replace the old statement with the new one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,你应该这样做。如果您只是使用
FileReader
,它会使用系统的默认字符编码。这是一个坏主意,这就是为什么我总是使用FileInputStream
和InputStreamReader
。您应该始终对此明确表示。如果您确实想要系统的默认字符编码,则应该使用Charset.defaultCharset()
- 但我强烈建议您不要这样做。如果您要读取文件,您应该知道字符编码并指定它。如果您要决定在写入文件时使用哪种字符编码,那么 UTF-8 是一个不错的默认选择。
Well, you should have done. If you were just using
FileReader
, it was using the default character encoding for the system. That was a bad idea, which is why I always usedFileInputStream
and anInputStreamReader
. You should always be explicit about it. If you really want the default character encoding for the system, you should useCharset.defaultCharset()
- but I strongly suggest that you don't.If you're going to read a file, you should know the character encoding, and specify that. If you get to decide what character encoding to use when writing a file, UTF-8 is a good default choice.
Java 中的
PrintWriter
/PrintStream
默认情况下具有 Charset.defaultCharset()PrintWriter
/PrintStream
in Java has by default Charset.defaultCharset()