如何检查Java程序的输入/输出流是否连接到终端? [英] How can I check if a Java program's input/output streams are connected to a terminal?

查看:239
本文介绍了如何检查Java程序的输入/输出流是否连接到终端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆