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);
}
}
}
jallalovski
0
Newbie Poster
Recommended Answers
Jump to PostCan you explain what happens when you try to compile and execute the program?
What does "not working" mean?
Post any error messages here.
All 3 Replies
NormR1
563
Posting Sage
Team Colleague
jallalovski
0
Newbie Poster
NormR1
563
Posting Sage
Team Colleague
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.