Search code examples
javaubuntustdin

Passing .txt indata running Java through terminal


So I have a Java-file e.g. example.java. I've then compiled it using javac (I then made a .class file in my directory, example.class).

I now want to run it using standard input with a .txt file in the ubuntu terminal. I remember it being fairly easily done with Python, but I'm not quite sure what to do with Java.

I want something along the lines of

dir$ java example indata.txt

That would pass my file indata.txt as standard input into my program.


Solution

  • I managed to solve my problem - the way I did it was as follows (in the terminal, standing in the same directory as my .java, .class and .txt files;

    java example < indata.txt