/**************************************************************************
/* ConcatenateFiles.java -- A tool to concatenate files on windows OS
/*
/* Copyright (C) 2003 Jean Marie ROUILLARD (jmrouill@umich.edu)
/*
/* This program is free software; you can redistribute it and/or modify
/* it under the terms of the GNU Library General Public License as published 
/* by  the Free Software Foundation; either version 2 of the License or
/* (at your option) any later version.
/*
/* This program is distributed in the hope that it will be useful, but
/* WITHOUT ANY WARRANTY; without even the implied warranty of
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
/* GNU Library General Public License for more details.
/*
/* You should have received a copy of the GNU Library General Public License
/* along with this program; see the file COPYING.LIB.  If not, write to 
/* the Free Software Foundation Inc., 59 Temple Place - Suite 330, 
/* Boston, MA  02111-1307 USA
/**************************************************************************/

import java.io.*;

public class ConcatenateFiles {

    static public void main (String arg[]) throws java.io.IOException{


	if (arg.length < 2) {
	    //Error message if the expected number of arguments is not provided
	    System.out.println("Syntax is java ConcatenateFiles destination_file file_1 file_2 file_3 ... file_n\n");
	}

	else {

	    int nbFiles = arg.length;

	    //Create output stream
	    PrintWriter saveAs = new PrintWriter(new FileOutputStream(arg[0]));

	    //Process all files that are not the desitnation file
	    for (int i=1; i<nbFiles; i++) {

		System.out.print("Processing " + arg[i] + "... ");
		
		//Create input stream
		BufferedReader readBuff = new BufferedReader (new FileReader(arg[i]));

		//Read each line from the input file
		String line = readBuff.readLine();
		
		while (line != null) {
		    saveAs.println(line);
		    line = readBuff.readLine();
		}
		readBuff.close();
		System.out.println(" DONE");	
	    }

	    saveAs.close();
	    System.out.println("All files have been concatenated into " + arg[0]);
	}
    }
}