简体   繁体   English

Java System.out output in Unicode

[英]Java System.out output in Unicode

I'm currently working on a text-based game in Java, which is played in the command line interpreter.我目前正在开发 Java 中的基于文本的游戏,该游戏在命令行解释器中播放。

The problem is, that I want to echo German umlaute (line ü ö ä ß ) in the command line, which isn't supported in Windows-systems by default.问题是,我想在命令行中回显德语变音符号(行ü ö ä ß ),默认情况下 Windows 系统不支持。 However, I found a way to achieve this with a bunch of things:但是,我找到了一种方法来实现这一点:

  • I changed the code-page to 65001 (using chcp 65001 )我将代码页更改为65001 (使用chcp 65001
  • I changed the font to Console Lucida我将字体更改为Console Lucida
  • I used a PrintStream with a UTF-8 encoding to print to the command line (in the Java-program).我使用带有 UTF-8 编码的PrintStream打印到命令行(在 Java 程序中)。

After doing this, outputting UTF-8 characters on the command line works.这样做之后,在命令行上输出 UTF-8 字符就可以了。 But this isn't the default setup on every PC.但这并不是每台 PC 上的默认设置。

So I thought, I create a Batch file to start the game, which does the basic setup to enable the UTF-8 output.所以我想,我创建一个批处理文件来启动游戏,它进行基本设置以启用 UTF-8 output。 Changing the code-page and using a UTF-8 PrintStream is no problem, but changing the font is kind of tricky.更改代码页并使用 UTF-8 PrintStream没有问题,但更改字体有点棘手。

The font should be installed on every Windows since Windows XP SP2, so that shouldn't be a problem.自 Windows XP SP2 以来的每个 Windows 上都应该安装该字体,所以这应该不是问题。 Also, you can change the font by changing a registry entry, but this requires some privileges and is not an option.此外,您可以通过更改注册表项来更改字体,但这需要一些权限并且不是一个选项。

I googled around but I didn't found any font -command.我四处搜索,但没有找到任何font命令。 Is there a workaround to change the console-font?是否有解决方法来更改控制台字体?

I experimented a bit and you can achieve this without any scripting and without JNI: Lucida can be chosen by making a link to cmd.exe and editing the properties of that link.我做了一些实验,你可以在没有任何脚本和没有 JNI 的情况下实现这一点:可以通过链接到 cmd.exe 并编辑该链接的属性来选择 Lucida。

I opened the link in a hex-editor to see that Lucide Console is chosen in there.我在十六进制编辑器中打开了链接,看到那里选择了 Lucide Console。 The really fun thing is, that inside the link the absolute path to cmd.exe is encoded (which is D:\Win... on my system), and then i copied the link to another computer i have here to see if it magically also works there (Windows is on C-Drive on that computer) and it does: Windows fixes the absolute path to cmd.exe for you and the console that is opened on that computer is set to Lucida too.真正有趣的是,在链接中 cmd.exe 的绝对路径被编码(在我的系统上是 D:\Win...),然后我将链接复制到我在这里的另一台计算机上看看它是否神奇地也在那里工作(Windows 在该计算机的 C 驱动器上)并且确实如此:Windows 为您修复了 cmd.exe 的绝对路径,并且在该计算机上打开的控制台也设置为 Lucida。

Now all you need to open your program inside that console (and make it print in that console) all you need is a batch-file containing the name of the link-file (ending in.lnk which is hidden on most systems) followed by your program name, like so:现在您只需要在该控制台中打开您的程序(并使其在该控制台中打印),您所需要的只是一个包含链接文件名称的批处理文件(以.lnk 结尾,在大多数系统上隐藏),然后是您的程序名称,如下所示:

c.lnk t.exe

since you are using java i guess you would have to write something like:由于您使用的是 java 我想您必须编写如下内容:

c.lnk java sth.jar

I created a minimal example you can find here:我创建了一个最小的例子,你可以在这里找到:

http://www.mediafire.com/?1yn40gsed3isjut http://www.mediafire.com/?1yn40gsed3isjut

it contains:它包含:

  • a minimal t.exe which just pops open a MessageBox (if you dont trust t.exe just open it in notepad or pedump and see it only calls MessageBox no other api-functions)一个最小的 t.exe,它只是弹出一个 MessageBox(如果您不信任 t.exe,只需在记事本或 pedump 中打开它,然后看到它只调用 MessageBox 而没有其他 api 函数)

  • the link c.lnk to cmd.exe configured to use Lucida, (the absolute path will be fixed by Windows on running it) c.lnk 到 cmd.exe 的链接配置为使用 Lucida,(绝对路径将由 Windows 在运行时固定)

  • the.bat file which calls t.exe through c.lnk通过 c.lnk 调用 t.exe 的.bat 文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM