Java read file by command line, <(less than) sy

2019-02-20 16:10发布

I am trying to read the filename by the command line,

This is command that our professor wants us to type:

java MultiBinaryClient xxxxxx.edu 6001 < files.txt

I was trying to use args[3] to get the file name, but args only contains "xxxxxx.edu" and "6001". why not "<" and "files.txt" in the args[]? Can anyone help me out?

BTW, I am using MAC terminal to test my code, I believe my professor uses win CMD, will it make differences?

Thank you!

3条回答
Lonely孤独者°
2楼-- · 2019-02-20 16:28

< is a redirection. The file will be streaming over stdin.

查看更多
贼婆χ
3楼-- · 2019-02-20 16:33

Let's see what each fragment means. This is how we execute a Java class containing a main method:

java MultiBinaryClient

The only command-line arguments that are being passed to your program are these ones:

xxxxxx.edu 6001

And this snippet is not part of the expected arguments to the Java program:

< files.txt

It's just Unix shell syntax to specify that the contents of files.txt must be read into your program via the standard input.

查看更多
家丑人穷心不美
4楼-- · 2019-02-20 16:33

You should escape the '<'

java MultiBinaryClient xxxxxx.edu 6001 \< files.txt
查看更多
登录 后发表回答