问题描述
我是Java的新手,如果这是通用知识,请原谅我,但我经过艰苦的搜索,找不到任何有用,相关或可理解的内容(考虑到我是C开发人员,这很奇怪!).我的问题是如何使Java在Windows Shell中打印Unicode字符串?".为了简单起见,我用另一种语言(例如:سلام")编写了hello world代码,并希望将其显示在外壳程序中(实际上,我也想获取Unicode,但首先必须弄清楚该代码).无需任何额外的代码行,就可以在Intellij IDEA中完美运行!
System.out.println(سلام");
但在shell中不起作用.
我非常失望,我从C迁移过来只是为了更好地处理Unicode!
我在Windows 10上使用了Intellij IDEA/Java 1.8,并以某种杂乱无章的方式尝试了很多事情,但是几乎可以正常工作.首先,这是代码:
import java.io.PrintStream;导入java.io.UnsupportedEncodingException;公共类Java901App {公共静态void main(String [] args){//System.out.println("Hello world!);//System.out.println(سلام");尝试{PrintStream outStream =新的PrintStream(System.out,true,"UTF-8");outStream.println("Hello world!");outStream.println(سلام");} catch(UnsupportedEncodingException e){System.out.println(捕获的异常:" + e.getMessage());}}}
-
请注意,PrintStream的编码设置为UTF-8.请参阅此帖子的选定答案:
-
因此可以正常工作,只是您提供的文本中的字符以相反的顺序呈现.有人知道如何解决这个问题,大概是通过在Java源代码中以某种方式指定文本是从右到左的语言来实现的?
更新:
我修改了代码,以便在命令提示符"窗口中正确显示波斯语文本,尽管这样做的副作用是,当在IDE中运行代码时,波斯语文本不再正确显示.这是修改后的代码:
public static void main(String [] args){尝试{StringBuilder persianHello =新的StringBuilder(سلام");PrintStream outStream =新的PrintStream(System.out,true,"UTF-8");outStream.println("Hello world!");outStream.println(persianHello);//在控制台中向后渲染,但在IDE中正确渲染.字节方向性= Character.getDirectionality(persianHello.charAt(0));如果(方向性== Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC){outStream.println(反向字符串:" + persianHello.reverse());//在控制台中正确渲染,但在IDE中向后...}} catch(UnsupportedEncodingException e){System.out.println(捕获的异常:" + e.getMessage());}}
这是使用该代码的命令提示符输出:
此修补程序是黑客;真正需要的是无论它们是从IDE还是命令提示符都可以正常运行的代码.
I'm new to java so excuse me if this is commong knowldge but I have searched hard and couldn't find anything helpful, relevant or understandable(Which is odd considering I'm a C developer!).My question is "How can I make java print a Unicode string in the windows shell ?". For simplicity say I have the hello world code in another language(ex: "سلام") and I want to display it in the shell (actually i want to get Unicode too, but first I have to figure this one out).This works perfectly in Intellij IDEA without any extra lines of code!
System.out.println("سلام");
but doesn't work in shell.
I'm seriously disappointed, I migrated from C just to get a better deal with Unicode!
I used Intellij IDEA/Java 1.8 on Windows 10 and tried a bunch of things in a somewhat disorganized manner, but have it almost working. First, here's the code:
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
public class Java901App {
public static void main(String[] args) {
//System.out.println("Hello world!");
//System.out.println("سلام");
try{
PrintStream outStream = new PrintStream(System.out, true, "UTF-8");
outStream.println("Hello world!");
outStream.println("سلام");
} catch(UnsupportedEncodingException e){
System.out.println("Caught exception: " + e.getMessage());
}
}
}
Note that the encoding for the PrintStream is set to UTF-8. See the selected answer for this post: Chinese Characters Displayed as Questions Marks in Mac Terminal
I uninstalled Arabic Script Supplemental Fonts and nothing changed so this step was not necessary.
Before running the app from the console I called chcp 65001. That was definitely essential even though the PrintStream was defined to use UTF-8, as shown in the screen shot below.
I tried setting different fonts for the Command Prompt window by clicking the icon in the top left of the window, selecting Defaults form the dropdown menu and then clicking the Fonts tab. Some worked (e.g. Consolas) and some didn't (e.g. MS Gothic). Note this comment from a SuperUser post: In order for chcp 65001 to work, you must be using a TrueType font in the command prompt.
Here's some sample output:
So it is working except that the characters in the text you provided are being rendered in reverse order. Does anyone know how to fix that, presumably by somehow specifying in the Java source that the text is for a right-to-left language?
Update:
I amended the code so that the Persian text is rendered correctly in the Command Prompt window, though a side effect is that it no longer renders correctly when the code is run within the IDE. Here's the revised code:
public static void main(String[] args) {
try{
StringBuilder persianHello = new StringBuilder("سلام");
PrintStream outStream = new PrintStream(System.out, true, "UTF-8");
outStream.println("Hello world!");
outStream.println(persianHello); // Renders backwards in console, but correctly in the IDE.
byte directionality = Character.getDirectionality(persianHello.charAt(0));
if (directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC) {
outStream.println("Reversed string:" + persianHello.reverse()); // Renders correctly in console, but backwards in the IDE...
}
} catch(UnsupportedEncodingException e){
System.out.println("Caught exception: " + e.getMessage());
}
}
And here's the Command Prompt output using that code:
This fix is a hack; what's really needed is code that will behave correctly regardless of whether it is run from the IDE or the Command Prompt.
这篇关于Unicode输出java Windows cmd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!