System.console() vs. System.out – System.console() vs. System.out

最后修改: 2020年 7月 31日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

In this tutorial, we’ll explore the differences between System.console() and System.out.

在本教程中,我们将探讨System.console()System.out之间的区别。

2. System.console()

2.System.console()

Let’s first create a program to retrieve the Console object:

让我们首先创建一个程序来检索Console对象。

void printConsoleObject() {
    Console console = System.console();
    console.writer().print(console);
}

Running this program from an interactive terminal will output something like java.io.Console@546a03af.

从交互式终端运行该程序将输出类似java.io.Console@546a03af的内容。

However, running it from other mediums will throw NullPointerException as console object would be null.

然而,从其他媒介运行它将抛出NullPointerException,因为控制台对象将是null

Or, if we run the program as below:

或者,如果我们按以下方式运行程序。

$ java ConsoleAndOut > test.txt

then the program will also throw a NullPointerException as we are redirecting the stream.

那么程序也将抛出一个NullPointerException,因为我们正在重定向流。

The Console class also provides methods to read passwords without echoing the character.

Console类还提供了读取密码的方法,而不需要回显字符。

Let’s see that in action:

让我们看看这一点的行动。

void readPasswordFromConsole() {
    Console console = System.console();
    char[] password = console.readPassword("Enter password: ");
    console.printf(String.valueOf(password));
}

This will prompt for the password, and it won’t echo the characters while we type it.

这将提示我们输入密码,而且在我们输入密码时不会有回音。

3. System.out

3.System.out

Let’s now print the object of System.out:

现在我们来打印System.out的对象。

System.out.println(System.out);

This will return something like java.io.PrintStream.

这将返回类似于java.io.PrintStream的东西。

The output will be the same from anywhere.

从任何地方输出的结果都是一样的。

System.out is used to print data to the output stream and there are no methods to read data. The output stream can be redirected to any destination such as file and the output will remain the same.

System.out用于向输出流打印数据,没有读取数据的方法。输出流可以被重定向到任何目的地,如文件,输出结果将保持不变。

We can run the program as:

我们可以以下列方式运行该程序。

$ java ConsoleAndOut > test.txt

This will print the output to the test.txt file.

这将打印输出到test.txt文件。

4. Differences

4.差异

Based on the examples, we can identify some differences:

根据这些例子,我们可以找出一些差异。

  • System.console() returns a java.io.Console instance when it’s run from an interactive terminal – on the other hand System.out will return java.io.PrintStream object irrespective of the invocation medium
  • The behavior of System.out and System.console() is similar if we haven’t redirected any streams; otherwise, System.console() returns null
  • When multiple threads prompt for input, then the Console queues up those prompts nicely – whereas in case of System.out all of the prompts appear simultaneously

5. Conclusion

5.总结

We learned in this article about the differences between System.console() and System.out. We explained that Console is useful when an application is supposed to run from an interactive console, but it has some quirks which should be noted and taken care of.

我们在这篇文章中了解了System.console()System.out之间的区别。我们解释说,当应用程序要从交互式控制台运行时,Console是很有用的,但它有一些怪癖,应该注意并加以处理。

As always, the complete code for this article is available over on GitHub.

一如既往,本文的完整代码可在GitHub上获得