views:

142

answers:

2

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.

A: 

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.

Pablo Santa Cruz
Joey
+2  A: 

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
McDowell
Awesome. I didn't know I could use parens to pipe multiple lines. Thanks.
YGL