・ About ʻInput Stream Reader (System.in)` which is taken care of by standard input -When I try to open a standard input stream that has been closed, it remains closed and does not open.
It is a standard input stream that is taken care of when inputting something from the console, but it seems that the standard input stream that was closed once in the same program cannot be opened.
For example
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test {
public static void main(String[] args) {
try {
//System when entering the second time.in remains closed
for (int i = 0; i < 2; i++) {
streamTest1();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*Returns the standard input string to the console as it is
* @throws IOException
*/
private static void streamTest1() throws IOException {
try (BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in))) {
System.out.print("input>");
String st = br1.readLine();
System.out.println("output>" + st);
}
}
}
When trying to do this
On the line String st = br1.readLine ();
in streamTest1
java.io.IOException: Stream closed
I found that the standard input stream was still closed.
As far as I can, when using multiple standard input streams in the same program, I can only think of closing them after the last stream is used. That is,
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test {
public static void main(String[] args) {
try {
streamTest1();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*Returns the standard input string to the console as it is
*
* @throws IOException
*/
private static void streamTest1() throws IOException {
//Try on the upper block of repetition-with-Bring a resources statement
try (BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in))) {
int i = 0;
while (i < 2) {
System.out.print("input>");
String st = br1.readLine();
System.out.println("output>" + st);
i++;
}
}
}
}
You can use the standard input stream repeatedly by doing.
-When I try to open and use a standard input stream that has been closed again, it remains closed and cannot be used. -Close the standard input stream for the time being after the last use of the stream.
... I think there is a way to close it and use it again. ..