Search code examples
javaintellij-idea

IntelliJ - Take standard input from a file


I need to take standard input from a file in IntelliJ IDEA.

$ java BinarySearch tinyW.txt < tinyT.txt 

'tinyT.txt' is the file which is to be taken as standard input. 'tinyW.txt' is another file which is passed as a command-line argument to the program.

How can this be achieved through IntelliJ?

PS: I am not running this program from the command-line as the class-path variable hasn't been set, and I am using functions from external libraries.


Solution

  • You need to create a "Run/Debug Configuration" (Menu "Run" -> "Edit Configurations...").

    About halfway down in the configuration panel is an entry "Redirect input from" with a checkbox before it. After selecting the checkbox you can select the file "tinyT.txt".

    "tinyW.txt" has to be entered into the field "Program arguments"


    Some time after writing this answer the Run/Debug configuration dialog has change. In 2023 if you want to redirect the input from some file you need to select "Modify Options" and then "Redirect Input"