Java Reference
In-Depth Information
reader = new FileReader(selectedFile);
. . .
}
501
502
A DVANCED T OPIC 11.2: Command Line Arguments
Depending on the operating system and Java development system used, there are
different methods of starting a programȌfor example, by selecting ȒRunȓ in the
compilation environment, by clicking on an icon, or by typing the name of the
program at a prompt in a terminal or shell window. The latter method is called
Ȓinvoking the program from the command lineȓ. When you use this method, you
must type the name of the program, but you can also type in additional information
that the program can use. These additional strings are called command line
arguments.
For example, it is convenient to specify the input and output file names for the
Line-Numberer program on the command line:
java LineNumberer input.txt numbered.txt
The strings that are typed after the Java program name are placed into the args
parameter of the main method. (Now you finally know the use of the args
parameter that you have seen in so many programs!)
When you launch a program from the command line, you can specify
arguments after the program name. The program can access these strings by
processing the args parameter of the main method.
For example, with the given program invocation, the args parameter of the
LineNumberer.main method has the following contents:
ȗ args[0] is Ðinput.txtÑ
ȗ args[1] is Ðoutput.txtÑ
The main method can then process these parameters, for example:
if (args.length >= 1)
inputFileName = args[0];
Search WWH ::




Custom Search