views:

220

answers:

2

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.

+3  A: 

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.)

Bombe
@Bombe: this doesn't address the core question ... which is how to tell if an EXISTING Stream is connected to a console.
Stephen C
What is an existing stream? How can a stream be connected to a terminal but not exist? Or are you trying to detect when a process loses its controlling terminal?
Bombe
@Bombe: Ah I see what the Console object does. But I still claim that this doesn't do what isatty does. Specifically, it does not tell you if a given stream is connected to the console.
Stephen C
+2  A: 

The short answer is that there is no direct equivalent of 'isatty' in standard Java. There's been a RFE for something like this in the Java Bug Database since 1997, but it only has one measly vote.

In theory, you might be able to implement 'isatty' using JNI magic. But that introduces all sorts of potential problems. I wouldn't even contemplate doing this myself ...

Stephen C
Thanks for the link to the RFE.
Zilk