Reputation: 5570
If I have a simple java program that processes lines of text from standard input, then I can run it with the following script:
@Echo off
java Test < file.txt
pause
exit
The script redirects lines of input from file.txt into the java program.
Is there a way that I can avoid having to use a separate file? Or is this the easiest way?
Thanks.
Upvotes: 3
Views: 4472
Reputation: 108899
Use a pipe.
This trivial Java app just prints out the lines from stdin
:
public class Dump {
public static void main(String[] args) {
java.util.Scanner scanner = new java.util.Scanner(System.in);
int line = 0;
while (scanner.hasNextLine()) {
System.out.format("%d: %s%n", line++, scanner.nextLine());
}
}
}
Invoked with this batch file:
@ECHO OFF
(ECHO This is line one
ECHO This is line two; the next is empty
ECHO.
ECHO This is line four)| java -cp bin Dump
PAUSE
...it will print:
0: This is line one
1: This is line two; the next is empty
2:
3: This is line four
Upvotes: 4
Reputation: 181290
You can do a simple:
$ java Test < file.txt
But if you absolutely need the pause
command, you will have to do it on a script, or code pause behavior in Test class.
Upvotes: 0