Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 7: What charset shall I use when calling Files.newBufferedReader?

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.

like image 888
dogbane Avatar asked Aug 09 '11 08:08

dogbane


1 Answers

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.

like image 161
Jon Skeet Avatar answered Nov 18 '22 07:11

Jon Skeet