如何指定与底层的Windows code页面一致的一个Java file.encoding的价值? [英] How do you specify a Java file.encoding value consistent with the underlying Windows code page?

查看:131
本文介绍了如何指定与底层的Windows code页面一致的一个Java file.encoding的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用的InputStreamReader 通过套接字接收数据的Java应用程序。它从 getEncoding 方法报告的Cp1252

  / * java.net。 * /插座袜子= ...;
InputStreamReader的是=新的InputStreamReader(Sock.getInputStream());
的System.out.println(字符编码=+ is.getEncoding());
//输出字符编码=的Cp1252

这不一定与什么系统报告,作为其code页面。例如:


C:\\> CHCP
主动code页:850

应用程序可能会收到字节0x81表示,这在code页850重新presents字符 U 。该计划除$ P $点与code页1252,这并不在该值定义任何字符字节,所以我得到一个问号代替。

我是能够解决这个问题谁在启动应用程序的批处理文件添加其他命令行选项一起使用code页850一个客户:


java.exe的 -Dfile.encoding = CP850 ...

但是,并非所有的客户使用code页850,当然。我怎样才能获取Java使用code页面与底层的Windows系统兼容吗?我的preference会是这样,我可以只是把批处理文件,留下了Java code不变:


ENC = ...
java.exe的-Dfile.encoding =%ENC%...


解决方案

的cmd.exe 使用的默认编码为 CP850 (或任何OEMCP原产于OS);该系统的编码是的Cp1252 (或任何ANSICP原产于OS)。 <一href=\"http://illegalargumentexception.blogspot.com/2009/04/i18n-uni$c$c-at-windows-command-prompt.html\">Gory这里细节。发现控制台编码的一种方法是做<一个href=\"http://illegalargumentexception.blogspot.com/2009/04/java-uni$c$c-on-windows-command-line.html\">via本土code (见 GetConsoleOutputCP 当前控制台编码;请参见 GetACP 以默认的ANSI编码; <一HREF =htt​​p://msdn.microsoft.com/en-us/library/aa383749%28VS.85%29.aspx>等)。

变更通过 -D 开关编码是会影响到所有的默认编码机制,包括重定向标准输出/标准输入/标准错误。它不是一个理想的解决方案。

我想出了这个WSH脚本,可以设置控制台系统ANSI codePAGE,但还没有想出如何以编程方式切换到TrueType字体。

 '文件:setacp.vbs
用法:CSCRIPT / NOLOGO setacp.vbs
设置objShell =的CreateObject(WScript.Shell)
更换ACP(ANSI)与OEMCP为默认的控制台CP
CP = objShell.RegRead(HKEY_LOCAL_MACHINE \\ SYSTEM \\ ControlSet001与&amp; _
                              \\控制\\ NLS \\ codePAGE \\ ACP)
WScript.Echo切换控制台code页面,与&amp; CP
objShell.Execchcp.com与&amp; CP

(这是我的第一个剧本WSH,因此它可能是有缺陷的 - 我不熟悉注册表读取权限)

使用TrueType字体是使用ANSI / UNI code与的cmd.exe 另一个要求。我要去一个纲领性的开关看起来更好的字体时,时间允许。

I have a Java application that receives data over a socket using an InputStreamReader. It reports "Cp1252" from its getEncoding method:

/* java.net. */ Socket Sock = ...;
InputStreamReader is = new InputStreamReader(Sock.getInputStream());
System.out.println("Character encoding = " + is.getEncoding());
// Prints "Character encoding = Cp1252"

That doesn't necessarily match what the system reports as its code page. For example:

C:\>chcp
Active code page: 850

The application may receive byte 0x81, which in code page 850 represents the character ü. The program interprets that byte with code page 1252, which doesn't define any character at that value, so I get a question mark instead.

I was able to work around this problem for one customer who used code page 850 by adding another command-line option in the batch file that launches the application:

java.exe -Dfile.encoding=Cp850 ...

But not all my customers use code page 850, of course. How can I get Java to use a code page that's compatible with the underlying Windows system? My preference would be something I could just put in the batch file, leaving the Java code untouched:

ENC=...
java.exe -Dfile.encoding=%ENC% ...

解决方案

The default encoding used by cmd.exe is Cp850 (or whatever "OEM" CP is native to the OS); the system encoding is Cp1252 (or whatever "ANSI" CP is native to the OS). Gory details here. One way to discover the console encoding would be to do it via native code (see GetConsoleOutputCP for current console encoding; see GetACP for default "ANSI" encoding; etc.).

Altering the encoding via the -D switch is going to affect all your default encoding mechanisms, including redirected stdout/stdin/stderr. It is not an ideal solution.

I came up with this WSH script that can set the console to the system ANSI codepage, but haven't figured out how to programmatically switch to a TrueType font.

'file:  setacp.vbs
'usage: cscript /Nologo setacp.vbs
Set objShell = CreateObject("WScript.Shell")
'replace ACP (ANSI) with OEMCP for default console CP
cp = objShell.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001" &_
                              "\Control\Nls\CodePage\ACP")
WScript.Echo "Switching console code page to " & cp
objShell.Exec "chcp.com " & cp

(This is my first WSH script, so it may be flawed - I'm not familiar with registry read permissions.)

Using a TrueType font is another requirement for using ANSI/Unicode with cmd.exe. I'm going to look at a programmatic switch to a better font when time permits.

这篇关于如何指定与底层的Windows code页面一致的一个Java file.encoding的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆