Java 通过Console对象打印是否更好?它处理;特殊字符“;更好?
我在书中读到以下内容:Java 通过Console对象打印是否更好?它处理;特殊字符“;更好?,java,character-encoding,console,character,command-prompt,Java,Character Encoding,Console,Character,Command Prompt,我在书中读到以下内容: import java.io.Console; // better to print thro' Console object - it handles "special characters" better class SpecialCharHandling { public static void main(String []args) { // string has three Scandinavian characters Stri
import java.io.Console;
// better to print thro' Console object - it handles "special characters" better
class SpecialCharHandling {
public static void main(String []args) {
// string has three Scandinavian characters
String scandString = "å, ä, and ö";
// try printing scandinavian characters directly with println
System.out.println("Printing scands directly with println: " + scandString);
// now, get the Console object and print scand characters thro' that
Console console = System.console();
console.printf("Printing scands thro' console's printf method: " + scandString);
}
}
以下是此程序打印的内容:直接使用
从这个输出中可以看到,控制台的printf()方法(以及其他
方法)更好地支持特殊字符
我尝试在本地启动此应用程序。我得到了一个不同的结果:
Printing scands directly with println: ├е, ├д, and ├╢
Printing scands thro' console's printf method: Г?, Г¤, and Г?
如何解释与书本的区别?奇怪,我直接用println:å、ä和ö打印Scans,然后
System.console()
返回null,我得到一个NPE。。。您是如何运行它的?@azurefrog Fornull
console(与此无关),@SotiriosDelimanolis谢谢,我在eclipse中没有注意到这个问题。从命令行中,我可以使用println:σ、∑和÷直接打印Scans,并通过控制台的printf方法:å、ä和ö打印Scans,因此它似乎高度依赖于您运行程序的方式。@azurefrog在IDEA中,我看到了相同的问题(null),您指的是哪本书?
Printing scands directly with println: ├е, ├д, and ├╢
Printing scands thro' console's printf method: Г?, Г¤, and Г?