如何检查Java程序的输入/输出流是否连接到终端? [英] How can I check if a Java program's input/output streams are connected to a terminal?
问题描述
我想要一个Java程序有不同的默认设置(详细,可能有色输出支持),具体取决于其用途。在C中,有一个isatty()函数,如果文件描述符连接到终端,则返回1,否则返回0。在Java中有这样的等价物吗?我没有在InputDream或PrintStream的JavaDoc中看到任何内容。
I would like a Java program to have different default settings (verbosity, possibly colored output where supported) depending on its use. In C, there is an isatty() function which will return 1 if a file descriptor is connected to a terminal, and 0 otherwise. Is there an equivalent for this in Java? I haven't seen anything in the JavaDoc for InputStream or PrintStream.
推荐答案
System.console()将返回您的应用程序连接到的控制台if它连接,否则返回 null
。 (注意,它只能从JDK 6上获得。)
System.console() will return the console your application is connected to if it is connected, otherwise it returns null
. (Note that it’s only available from JDK 6 on.)
这篇关于如何检查Java程序的输入/输出流是否连接到终端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!