I wrote a tiny Press Enter to Continue module but Java won't let me close the BufferedInputStream after I read from it. If I close it it errors "Stream closed" when I try to call the module again, even though I try to open it again first.
import java.io.*;
final public class PressEnter {
public static void toContinue() {
try {
BufferedInputStream buff=new BufferedInputStream(System.in);
int in=buff.read();
buff.close();
}
catch (IOException e) { System.out.println("Error: "+e.getMessage()); }
}
}
I don't close the BufferedInputStream and it works fine, as many times as I call it. Leaving in buff.close it errors.
public class TestPressEnter {
public static void main(String[] args) {
System.out.print("Press Enter To Continue...");
PressEnter.toContinue();
System.out.println("Continuing...");
System.out.print("Press Enter To Continue Again...");
PressEnter.toContinue();
System.out.println("Continuing...");
}
}