FileReader
and FileWriter
are built-in APIs for writing text to the local filesystem. They're extremely convenient and concise:
FileReader reader = new FileReader(file);
Sadly, this code is broken, and there's no way to use FileReader or FileWriter in a way that isn't broken. When these classes were designed in 1997, the file was king. You could export a .txt file from WordPerfect and read it in Word '95. These 1990's programs had no mechanism to negotiate a charset, so the convention was to always use the host operating system's default. Computers in Italy would use different charsets than computers in Poland. This worked, as long as the files never travelled too far.
The Internet happened. Our files learned new ways to travel, like email and Dropbox. It no longer makes sense to choose character sets that assume a quarantine.
But FileReader and FileWriter still insist on using the diminishingly-relevant platform charset. And this makes these classes obsolete in 2012. Instead, always use UTF-8. This code isn't as convenient or concise, but it is correct:
Reader reader = new InputStreamReader(new FileInputStream(file), "UTF-8");
With encouragement of Jake Wharton, I'm working on improving Gson so it's got all three: convenient, concise and correct.
See also Elliott's excellent post on the subject.