CS 313e - Command Line
Arguments
Feedback questions:
0. How well do you understand the sorting algorithms?
1. Write down the algorithm for selection sort. Assume you want to sort
the elements of an array A of N elements into ascending order.
Command Line Arguments
Command-line arguments = strings that follow the name of the
application program on the command line when the program is executed.
Ex: Suppose you have a program called FileCopy that copies one file to
another. To compile this program, you type
javac FileCopy.java
To run this program, you type:
java FileCopy oldFile.txt newFile.txt
^
^
args[0] args[1]
Note: The command line arguments are Strings - they are passed to the
main method of your program in the String array called args.
Recall the main method header: public static void main(String[] args)
Ex: The PrintWriter object for your FileCopy program
PrintWriter writer = new PrintWriter(new BufferedWriter(new
FileWriter(args[1])));
Exercise: Write the FileCopy program.
Hint: Check the number of command line arguments,
and print an error message if it's incorrect.
Example: Write a program that takes a word on the command line and
prints it to the screen.