views:

485

answers:

4
+2  Q: 

Encoding cp1252

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.

A: 

I believe this encoding is set by the JVM so it wouldn't make sense to retrieve it from outside

yankee2905
No, it's not, this is a Windows encoding for command line tools
Kico Lobo
Kico: Not, it's not. The codepage used on the command line is yet a different one.
Joey
+2  A: 

That value is, on Windows at least, the legacy codepage used for non-Unicode text. It's what the OS converts strings to and from when you use the old ANSI APIs. For any newer program it should have no effect (that being said, I still see enough programs that use the A and not the W variants of API functions, sadly).

For you Java program none of that should matter, as Java uses Unicode exclusively. If you want to write or read text files in the system's codepage, then you'll need it, however.

For the command prompt, however, that encoding is of no significant value, as the console by default uses the OEM encoding which mimics the one of the DOS ages (850 or 437 is pretty common).

Joey
A: 

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.

Dan
A: 

Since this doesn't really have anything to do with Java, you could just opt to use a WSH script:

' save this script as printANSI.vbs
' usage: cscript /Nologo printANSI.vbs
Set objShell = CreateObject("WScript.Shell")
cp = objShell.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001" &_
                              "\Control\Nls\CodePage\ACP")
WScript.Echo cp

See also the chcp command; you may want to read up on how encoding works on the Windows command prompt (some links in this blog post).

McDowell
thanks mcdowell, this was useful
Arun