Obtaining the default charset (aka platform encoding) in JAVA

… is really simple since JDK 1.5:

java.nio.charset.Charset.defaultCharset()

So please don’t use messy, old workarounds such as this:

byte [] byteArray = {'a'};
InputStream inputStream = new ByteArrayInputStream(byteArray);
InputStreamReader reader = new InputStreamReader(inputStream);
String defaultEncoding = reader.getEncoding();

Which, unfortunately, is one of the first things you stumble upon when searching for this topic.

This entry was posted on Monday, November 9th, 2009 at 11:57. Posted in: , java, open source. You can follow any responses to this entry through the RSS 2.0feed. You can leave a response, or trackback from your own site.

Leave a Reply