Encoding cp1252

Arun picture Arun · Dec 1, 2009 · Viewed 27.3k times · Source

When I try the following in Java:

System.out.println(System.getProperty("file.encoding"));

I get cp1252 as the encoding.

Is there a way to know where this value is coming from? (Like Environment variables or something)

I would like to print the value of encoding on command prompt using some command like systeminfo on Windows XP.

Answer

Dan picture Dan · Dec 1, 2009

cp1252 is the default encoding on English installations of MS Windows (what Microsoft refers to as ANSI). Java by default will take the system locale as its default character encoding. What this means is system dependent. In general I don't like to rely on default encodings. If I know my text will be pure ASCII I ignore it - otherwise I set the encoding explicitly when instantiating InputStreamReader, OutputStreamWriter, String etc or calling getBytes.

Note that cp1252 is not the default encoding on the Windows command prompt. That is the even older cp437, which you can see (and change) using the chcp command.