import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;


    public class Concat {

        public boolean accept(File file) {
            //if the file extension is .txt or .java return true, else false
            if (file.getName().endsWith(".txt")||file.getName().endsWith(".java")) {
                return true;
            }
            return false;
        }

        public void copy(PrintWriter pw, String inputFile)
        {
            File f = new File(inputFile);
            if(accept(f)==true){

                try {
                    Scanner scan = new Scanner(f);
                    pw= new PrintWriter(new FileOutputStream("Stuff.txt"));
                    while (scan.hasNextLine())
                    {
                        String s = scan.nextLine();
                        pw.println(s);
                    }
                    scan.close();
                } catch (FileNotFoundException e) {
                    System.out.println(" Error: "+e + " "+ inputFile);
                }
            }else{

                System.out.println("File name must be a TEXT Or JAVA type");

            }
        }

        // The routine will throw a RuntimeException if the inputFile name does 
        // note end with .java or .txt

        // This routine will also throw a RuntimeException if any problem occurs in 
        // opening the inputFile

        // The copy routine copies the contents of the inputFile into the PrintWriter


        public void concat(String outFile, String[] inputFiles)
        {
            try {
                PrintWriter writer = new PrintWriter(outFile);
                for(int i=0;i<inputFiles.length;i++){
                    String source=inputFiles[i];
                    copy(writer,source);
                }

            } catch (IOException e) 
            {
                System.err.println(e);
                System.exit(1);
            }

            // Fill in details.
            // The routine will call copy to do a copy of a single file
            // This routine will also create an open PrintWriter for the outFile
            // If any problem occurs, a RuntimeException should be thrown with 
            // appropriate error information
        }


        public static void main(String[] args) {
            if (args == null || args.length < 2)
            {
                System.out.println("Concat needs at least 2 filenames");
                return;
            }
            try
            {

                String[] inputFiles = new String[args.length-1];
                for (int i=0; i < inputFiles.length; i++)
                    inputFiles[i] = args[i+1];
                Concat c = new Concat();
                c.concat(args[0], inputFiles);
            }
            catch (RuntimeException e)
            {
                // Possible errors:
                // An input file that doesn't end with .java or .txt
                // An input file that doesn't open properly.
                // Trouble creating the output file

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

Can you explain what happens when you try to compile and execute the program?
What does "not working" mean?
Post any error messages here.

the program will creat an empty file called stuff.tx but will not copy the file that i want it to copy

will not copy the file

Are you sure the program is reading from the input file and writing what is read to the output file?

Try debugging the code by adding some println statements that print out the values of variables as they are given values and used as the program executes and shows the execution flow of the code so you know what the code is doing as it executes.

This article has been dead for over six months. Start a new discussion instead.