In Java How to Re-Open System.In After Closing It

In Java is it possible to re-open System.in after closing it

It is not possible to reopen System.in, System.out or System.err. The underlying native streams are file descriptors that are connected to other processes, or to files whose identity your application cannot discern. Once the underlying native file descriptors are closed, it is not possible to reopen them.

The best I can suggest is that you create a wrapper InputStream class for the System.in object, and code the wrapper to treat close() as a no-op. Or maybe set the wrapper into a "closed" state without actually closing the wrapped stream.

In your specific use-case, that won't work, because you "need" to unblock the thread that is blocked while reading from System.in. So in your case, you will need to do non-blocking input from System.in. For example, use the available() method to test if there are any characters to read from the console. (It is typically safe to assume that if available() returns a number greater than zero you will be able to read an entire line.)

(It might also be able to implement non-blocking reads using a Selector, but I don't think that it is possible to obtain a "selectable channel" for the System.in object.)


Note that Thread.interrupt() won't work. According to the javadocs, it will only work if you are reading from an interruptible channel.

  • System.in is not an interruptible channel, and

  • if it was, then the documented behaviour for interrupt() is that the channel gets closed by the interrupt.

System.out closed? Can I reopen it?

The general contract for OutputStream's close:

public void close()
throws IOException Closes this output stream and releases any system resources associated with this stream. The general contract
of close is that it closes the output stream. A closed stream cannot
perform output operations and cannot be reopened.

PrintStream's

public void close() Close the stream. This is done by flushing the
stream and then closing the underlying output stream.

The only advice I can give you is that you should not write asymmetrical code, that is, don't delegate the closing of resources your code has created to somewhere else.

Even if in your case it might seemed sensible to close the wrapper stream, the fact is that you should not because you are closing a stream opened somewhere else.

In short:

public void write(String txt, OutputStream out) {
PrintWriter printer = new PrintWriter(out);
printer.print(txt);
printer.flush();
//it is very unpolite to close someone else's streams!
//printer.close();
}

Oh, and by the way, you may want to change the function name to print, rather than write.

NoSuchElementException on reopening the scanner within a method

There is no such thing as "reopening a stream". The only thing you can do is create a new stream, with a new Scanner, and optionally place it in the same variable.

Once you close sc, you also close the underlying System.in stream. Creating a new Scanner on top of it can't "reopen" it, and since the stream is closed, you get this exception.

To make a long story short - while it's definitely a good practice to close resources when you're done with them, you shouldn't close System.in (or any scanner based on it).

Recover data after closing a Jar file

Your program's execution is over when you close the window. Consider the .jar file as your executable program handled by JRE. To be able to save your data you may write it to the file system or a database. You can serialize your state and save it to a file then load from the file on reopening.

Java tutorial for Reading, Writing, and Creating Files

Stream closed and not reopened - Java

Your problem is indeed due to the try-with-resource statement that closes new InputStreamReader(System.in) which behind the scene closes also the underlying input stream that is System.in (in is a public static field of System) such that in your modify method System.in is already closed and then cannot be read anymore this is why you get this exception.



Related Topics



Leave a reply



Submit