6

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.

dogbane
  • 266,786
  • 75
  • 396
  • 414

2 Answers2

12

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Yes, I will do that going forward. (In my defence, none of the IO tutorials used a charset.) For now, I just want to upgrade my code such that all the files I was reading previously, continue to be read in the same way as they did before, without any unexpected results. Shall I use `defaultCharset()` then? – dogbane Aug 09 '11 at 08:39
  • 3
    Yes, `defaultCharset()` will give you the previous behaviour. Just so long as you're aware that it's almost certainly not the right decision... – Jon Skeet Aug 09 '11 at 08:42
0

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

java.nio.charset.Charset.defaultCharset()
oliholz
  • 7,447
  • 2
  • 43
  • 82
  • 2
    No, PrintWriter uses whatever the default character encoding has been determined as. On *some* systems that will be UTF-8, on others it will be something completely different. – Jon Skeet Aug 09 '11 at 08:26
  • oh sorry, my fault, that was not my intention, only want to say: use `defaultCharset()` – oliholz Aug 09 '11 at 08:32