How to "reset" or "clear" System.in using java application?

I ran my java application in about the same way as below:

InputStreamReader isReader = new InputStreamReader(System.in);
BufferedReader bufReader = new BufferedReader(isReader);
while (true) {
    try {
        String inputStr = null;
        if ((inputStr=bufReader.readLine()) != null) {
            //DO SOMETHING
        }
        else {
            System.out.println("input is null");
        }
    }
    catch (Exception e) {
        //DO SOMETHING
    }
}

      

using this command java test < textfilename

but after everything is read the application starts to print input is null

. Of course I can add break;

below System.out.println("input is null");

, but is there a way that can clear the input buffer and start getting input from the command line?

PS: Example file I'm using:

11111111
11111112
11111113
11111114

      

+3


source to share


3 answers


As far as I know, this is not possible. When you use <

the command line to read from a file, the content is System.in

controlled by the shell, not Java.



readLine()

return null

is how Java signals that you have reached the end of the file, as well as what you get if you send EOF

/ type Ctrl-D to the shell.

+2


source


You cannot redirect input or output and access the normal console System.in

. From the docs (emphasis mine):

Whether a virtual machine is a console depends on the underlying platform as well as how the virtual machine is invoked. If a virtual machine is started from an interactive command line without redirecting standard input and output streams , then its console will exist and will usually be connected to the keyboard and display from which the virtual machine was launched.

However, you can pretty much accomplish what you want with minimal changes to your code.



public static void main(String args[]) throws FileNotFoundException {
    readFile(args[0]);
    // Continue normal console input here
}

private static void readFile(String filename) throws FileNotFoundException {
    InputStream original = System.in;
    System.setIn(new FileInputStream(filename)); // VM trickery

    try (BufferedReader bufReader = new BufferedReader(new InputStreamReader(System.in))) { 
        String inputStr = null;
        while ((inputStr = bufReader.readLine()) != null) { // stops loop on `null`
            System.out.println(inputStr);
        }
    } catch (IOException e) { 
        e.printStackTrace();
    }

    System.setIn(original); // more VM trickery
}

      

Major changes:

  • Called with the filename of the input file as the first argument, i.e. java test textfilename

  • Uses FileInputStream
  • Using try-with-resources to ensure the FileInputStream is added I closed
  • Handles a zero return readLine()

    for an infinite loop onnull

+1


source


Perhaps something like this:

InputStreamReader isReader = new InputStreamReader(System.in);
BufferedReader bufReader = new BufferedReader(isReader);
String inputStr = null;
try {
   while ((inputStr=bufReader.readLine()) != null) {
   //DO SOMETHING
   }
}catch (Exception e) {
//DO SOMETHING
}

      

0


source







All Articles