I am writing a simple Java project call Robot. The Robot can accept a config file from the command prompt (If there is not file, the robot will use the default config) by:
java Robot < config.txt
Does anyone know how I can read the redirect input from the command prompt in java?
You can access System.in
as an input stream, that will contain all redirected input.
Specifically, reading from a redirected stdin is the same as reading from the regular stdin.
For example, let's say you want to read a list of integers from a redirected stream.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Foo {
public static void main(String args[])
throws IOException
{
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
int sum = 0;
String line;
while ((line = r.readLine()) != null) {
sum += Integer.parseInt(line);
}
System.out.println(sum);
}
}
Then when you run this, you'll get
$ (echo 1; echo 2; echo 65) > foo.txt
$ java Foo < foo.txt
68
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.