Java控制台应用程序:System.out仍然是要走的路? [英] Java console applications: Is System.out still the way to go?
问题描述
使用 System.out
(和相关)总是看起来很尴尬,因为公共领域,现在与最新的Netbeans给了一个明确的语句应该删除提示。 / p>
因此我的问题: System.out
仍然是写入控制台或是有任何其他API
>在设计为从命令行运行的命令中,使用 System.out
和 System.err
用于输出和运行命令的人可以合理期望理解的错误消息。如果你从一个体面的shell或脚本运行你的应用程序, System.out
和/或 System.err
streams可以重定向到管道中的文件或另一个进程。 AFAIK,使用 System.out
和 System.err
是尊重用户的愿望重定向。因此,对于大多数使用情况,这仍是命令行应用程序为用户写入输出的最佳方式。
在JDK 1.6及更高版本中,还有一个类 java.io.Console
,这将允许您访问控制台。它在Javadoc中没有说明,但我怀疑 java.io.Console
向控制台打开一个新的流,因此无法重定向。 (在旧式UNIX盒子上打开/ dev / tty的道德等同)。如果你的应用程序想要真正地与用户通话,而不能重定向的话,你应该使用它。例如
但是,如果你的应用程序是GUI命令,特别是如果它打算作为无人值守服务运行,你应该避免写入控制台,因为:
- 控制台流(
System。{out,err}
或java.io.Console
)可能未连接任何内容,或 - 用户不会注意到控制台输出,控制台窗口埋入 1 或
- ,控制台输出可能会使用户感到厌烦,尤其是如果您的应用程序没有办法抑制它。
1 - 根据控制台输出的性质,用户想要什么。但是应用程序员需要考虑这一点。
Using System.out
(and related) always seemed awkward because of the public field and now with the latest Netbeans gives a blatant "Statement should be removed" hint.
Hence my question: Is System.out
still the preferred way to write to the console or is there any other API which should be preferred?
EDIT: I don't want this for logging, but for a simple console application. Something like ps
which prints out the running processes to the console.
In a command that is designed to be run from the command line, it is (IMO) reasonable to use System.out
and System.err
for output and for error messages that the person running the command could reasonably expect to understand. If you are running your application from a decent shell or script, the System.out
and/or System.err
streams may be redirected to files or another process in a pipeline. AFAIK, using System.out
and System.err
is the only way to "respect" the user's wishes as far as redirection is concerned. Hence, for most use-cases this is still the best way for a command line application to write output for the user to see.
In JDK 1.6 and later, there also a class called java.io.Console
which will give you access to the console. It is not spelled out in the Javadoc, but I suspect that java.io.Console
opens a fresh stream to the console, and therefore cannot be redirected. (The moral equivalent of opening "/dev/tty" on an old-school UNIX box.) IMO, you should use this if your application wants to REALLY talk to the user, without the possibility of redirection; e.g. to ask for and then read a password.
However, if your application is GUI command, and particularly if it is intended to run as an unattended service, you should avoid "writing to the console" because:
- the console streams (
System.{out,err}
orjava.io.Console
) may be connected to nothing, or - the may user not notice the console output because the console window is buried1, or
- the console output may end up annoying the user, especially if your application provides no way to suppress it.
1 - Depending on the nature of the console output, this may actually be precisely what the user wants. But the application programmer needs to take this into account.
这篇关于Java控制台应用程序:System.out仍然是要走的路?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!