java default encoding toString UTF8

The default is the derived from the underlying OS. It can be overridden by a system property (file.encoding).

Since both of these are kind of outside of your control, it is probably better to explicitly specify an encoding for all the files your program writes and read. UTF-8 should work in most cases.

Stream tostring() method uses JVM default encoding, which JVM gets from the current OS if not explicitly specified.

Like

  byteArrayOutputStream baos = new ByteArrayOutputStream();
  ...do something
  baos.toString();

if the outputString uses UTF-8 and it is in windows, given there are extended chars, it would messed up.

Solution:

  1. we can explicitly say outputStream.toString(“UTF-8”);
  2. we can also run our JVM using parameter: java -Dfile.encoding=UTF8 myClass
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s