Are Java System.in/out/err always open?

113 Views Asked by At

Javadoc says in, out and err are 'already open', I just wonder that do they close after program finishes? If not does it contradict the practise to close any I/O stream references after use?

2

There are 2 best solutions below

0
SeverityOne On BEST ANSWER

stdin, stdout and stderr are very much a Unix concept, but other operating systems (such as Windows) have copied the concept as well. These are maintained by the system, and you usually don't close them.

However, you can close them, and you even have to close them when you fork() a program, which is the Unix way to have a program detach from the command line and run in the background, such as a daemon.

System.in/out/err follow this strategy. So you could close them, but you don't have to, because they're special system resources.

2
Sagar Pudi On

System.in / out / err these classes are Instantiated by the JVM, not by us. Hence JVM will take care of closing them.