I have a programming question that calls for me to write a program that concatenates the contents of several files into one file. For example,
java CatFiles chapter1.txt chapter2.txt chapter3.txt book.txt
makes a long file, book.txt, that contains the contents of the files chapter1.txt, chapter2.txt, and chapter3.txt. The output file is always the last file specified on the command line.
The main class from my instructor
import java.io.IOException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Scanner;
/**
This program concatenates contents of several files into one file.
*/
public class CatFiles
{
public static void main(String[] args) throws IOException
{
if (args.length < 2)
{
System.out.println(
"Usage: CatFiles sourcefile1 sourcefile2 . . . targetfile");
return;
}
String target = args[args.length - 1] ;
. . . //Write code
for (int i = 0; i < args.length - 1; i++)
{
String source = args[i];
. . . //Write code
}
. . . //Write code
}
}
What I am having trouble with trying to figure out where exactly to put my Filereader, PrintWriter, and Scanner. Have not had many problems using the args in this class so that is why it is confusing. Any help would be appreciated.