
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);
}
}
}

I am not really sure why This program is not running properly? Any ideas? Thanks in advance.

Here is an example of concatenating text files.
import java.io.*;
import java.util.*;
public class Concat {
static public void main(String arg[]) throws java.io.IOException {
PrintWriter pw = new PrintWriter(new FileOutputStream("c:/concat.txt"));
Scanner input=new Scanner(System.in);
System.out.print("How many files you want to concat?:");
int no=input.nextInt();
String f[]=new String[no];
System.out.println("Enter filenames: ");
for(int i=0;i<f.length;i++){
String filename=input.next();
int mid= filename.lastIndexOf(".");
String ext=filename.substring(mid+1,filename.length());
if(ext.equals("txt")){
f[i]=filename;
System.out.println("Processing " + f[i] + "... ");
BufferedReader br = new BufferedReader(new FileReader(f[i]));
String line = br.readLine();
while (line != null) {
pw.println(line);
line = br.readLine();
}
br.close();
}
else{
System.out.print("Only txt files should be concated.Re-enter file name: ");
}
}
pw.close();
System.out.println("All files have been concatenated into concat.txt");
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.