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?
Asked
Active
Viewed 80 times
1

Sagar Pudi
- 4,634
- 3
- 32
- 51

mzoz
- 1,273
- 1
- 14
- 28
-
1You know, I'm not sure. I certainly would expect all streams to be closed on exit (think of a JVM that is part of a pipe, it's got to signal end of input/output), but what happens if the JVM exits with an error? If the error is severe enough I'm sure that streams won't be flushed and closed, it's always possible to lose data. – markspace Apr 09 '18 at 04:40
2 Answers
1
System.in / out / err these classes are Instantiated by the JVM, not by us. Hence JVM will take care of closing them.

Sagar Pudi
- 4,634
- 3
- 32
- 51
-
1
-
Yes, It is possible to close them manually but not necessary to do this. – Sagar Pudi Apr 09 '18 at 04:52
1
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.

SeverityOne
- 2,476
- 12
- 25