File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes how to get platforms default encoding? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to get platforms default encoding?" Watch "how to get platforms default encoding?" New topic

how to get platforms default encoding?

Anamitra Bhattacharyya

Joined: May 20, 2002
Posts: 5
Hi guys
Is there any way to get the platoforms default character encoding? I looked into the source code of the String class getBytes() method - but it uses some class to determine the encoding - which I cannot use as that is not a public API.
Any help/pointers is appreciated.
Jessica Sant

Joined: Oct 17, 2001
Posts: 4313

From Java Internationalization by Deitsch and Czarnecki; Ch 6 Character Sets and Unicode, pg 163
The value of the system property, file.encoding, determines the default character encoding used by the character stream classes*.
* file.encoding is not part of the Java specification, and is therefore an implementation-dependent feature.

System.out.println( System.getProperty( "file.encoding" ) );
Will give you what you want *if* that property exists.
[ June 18, 2002: Message edited by: Jessica Sant ]

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
I've used
String enc = new OutputStreamWriter(System.out).getEncoding();
It's not pretty, but at least it's supported by the API. Note that the only purpose to System.out here is to supply some sort of OutputStream for the Writer constructor.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: how to get platforms default encoding?
It's not a secret anymore!