Java 7:调用 Files.newBufferedReader 时应使用什么字符集?

发布于 2024-11-28 18:10:58 字数 643 浏览 2 评论 0原文

在以前版本的 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 技术交流群。

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

发布评论

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

评论(2

稀香 2024-12-05 18:10:58

以前,我在读取纯文本文件时不必担心字符集。

好吧,你应该这样做。如果您只是使用 FileReader,它会使用系统的默认字符编码。这是一个坏主意,这就是为什么我总是使用 FileInputStreamInputStreamReader。您应该始终对此明确表示。如果您确实想要系统的默认字符编码,则应该使用Charset.defaultCharset() - 但我强烈建议您不要这样做。

如果您要读取文件,您应该知道字符编码并指定它。如果您要决定在写入文件时使用哪种字符编码,那么 UTF-8 是一个不错的默认选择。

Previously, I did not have to worry about charsets when reading plain text files.

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 used FileInputStream and an InputStreamReader. You should always be explicit about it. If you really want the default character encoding for the system, you should use Charset.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.

最笨的告白 2024-12-05 18:10:58

Java 中的 PrintWriter/PrintStream 默认情况下具有 Charset.defaultCharset()

java.nio.charset.Charset.defaultCharset()

PrintWriter/PrintStream in Java has by default Charset.defaultCharset()

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