63.0k views
0 votes
Please add comments to your program to explain it. Thank you!

In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, sort them, and write them to an output file.

1. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. For example, assume your package name is FuAssignment5 and your main class name is FuMain, and your executable files are in "C:\Users\2734848\eclipse-workspace\CIS 265 Assignments\bin". The following command line will read from a local file "students.txt" and write to a local file "students_sorted.txt": C:\Users\2734848\eclipse-workspace\CIS 265 Assignments\bin > java FuAssignment5.FuMain students.txt students_sorted.txt

2. If the program is run with incorrect number of arguments, your program must print an error message and exit. The error message must show correct format to run your program, e.g., "Usage: FuAssignment5.FuMain input_file output_file" where FuAssignment5 is the package and FuMain is the main class.

3. Each line in the input file represents a student. There are 5 fields in each line: name, id, gpa, "graduate" or "undergraduate", isTransfer (for undergraduate) or college (for graduate). The fields are separated by comma, ",". For example, the input file students.txt file may contain: Michelle Chang,200224,3.3,graduate,Cleveland State University Tayer Smoke,249843,2.4,undergraduate,false David Jones,265334,2.7,undergraduate,true Abby Wasch,294830,3.6,graduate,West Virginia

4. The program will read the lines and create undergraduate students or graduate students accordingly. The students are added to an ArrayList.

5. The program then sort the students in ArrayList in ascending order of their id.

6. The sorted list of students is written to the output file.

1 Answer

6 votes

Answer / Explanation:

(1) We should first understand that the input filename are passed in as the first command arguments at command line, respectively.

To do this, we import the data file:

So we have,

import java.io.*;

/**

* Makes a copy of a file. The original file and the name of the

* copy must be given as command-line arguments. In addition, the

* first command-line argument can be "-f"; if present, the program

* will overwrite an existing file; if not, the program will report

* an error and end if the output file already exists. The number

* of bytes that are copied is reported.

*/

public class CopyFile {

public static void main(String[] args) {

String sourceName; // Name of the source file,

// as specified on the command line.

String copyName; // Name of the copy,

// as specified on the command line.

InputStream source; // Stream for reading from the source file.

OutputStream copy; // Stream for writing the copy.

boolean force; // This is set to true if the "-f" option

// is specified on the command line.

int byteCount; // Number of bytes copied from the source file.

/* Get file names from the command line and check for the

presence of the -f option.

(2) If the command line is not one

of the two possible legal forms, print an error message and

end this program. */

if (args.length == 3 && args[0].equalsIgnoreCase("-f")) {

sourceName = args[1];

copyName = args[2];

force = true;

}

else if (args.length == 2) {

sourceName = args[0];

copyName = args[1];

force = false;

}

else {

System.out.println(

"Usage: java CopyFile <source-file> <copy-name>");

System.out.println(

" or java CopyFile -f <source-file> <copy-name>");

return;

}

/* Create the input stream. If an error occurs, end the program. */

try {

source = new FileInputStream(sourceName);

}

catch (FileNotFoundException e) {

System.out.println("Can't find file \"" + sourceName + "\".");

return;

}

/* If the output file already exists and the -f option was not

specified, print an error message and end the program. */

File file = new File(copyName);

if (file.exists() && force == false) {

System.out.println(

"Output file exists. Use the -f option to replace it.");

return;

}

/* Create the output stream. If an error occurs, end the program. */

try {

copy = new FileOutputStream(copyName);

}

catch (IOException e) {

System.out.println("Can't open output file \"" + copyName + "\".");

return;

}

(3) /* Copy one byte at a time from the input stream to the output

stream, ending when the read() method returns -1 (which is

the signal that the end of the stream has been reached). If any

error occurs, print an error message. Also print a message if

the file has been copied successfully. */

byteCount = 0;

try {

while (true) {

int data = source.read();

if (data < 0)

break;

copy.write(data);

byteCount++;

}

source.close();

copy.close();

System.out.println("Successfully copied " + byteCount + " bytes.");

}

catch (Exception e) {

System.out.println("Error occurred while copying. "

+ byteCount + " bytes copied.");

System.out.println("Error: " + e);

}

} // end main()

} // end class CopyFile

User Eelke
by
7.4k points