September 3, 2009 at 4:18 PM
Hi Friend,
Try the following code:
import java.io.*;
import java.util.Scanner;
class FileConcat {
public static void main (String args []) throws IOException {
Scanner stdin = new Scanner (System.in);
System.out.print ("Enter two text file name with path: ");
String filename1 = stdin.next();
String filename2 = stdin.next();
File file=new File (filename1);
FileInputStream fstream = new FileInputStream(file);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
FileWriter fostream = new FileWriter("Master.txt",true);
BufferedWriter output = new BufferedWriter(fostream);
output.write(strLine);
output.newLine();
output.close();
}
File file1=new File (filename2);
FileInputStream fstream1 = new FileInputStream(file1);
DataInputStream in1 = new DataInputStream(fstream1);
BufferedReader br1 = new BufferedReader(new InputStreamReader(in1));
String strLine1;
while ((strLine1 = br1.readLine()) != null) {
FileWriter fostream1 = new FileWriter("Master.txt",true);
BufferedWriter output1 = new BufferedWriter(fostream1);
output1.write(strLine1);
output1.newLine();
output1.close();
}
}
}
Thanks
Related Tutorials/Questions & Answers:
FileHandling - Java BeginnersFileHandling
Q. Write a java prg which accepts a list of existing text files from command line args & concatinate contents of all files in the "Master.txt".
I have tried to solve it but with errors. Kindly ammend the existing
Advertisements
Create File in Java_TO_REPLACE_1
Example of how to create a file in java:
package
FileHandling;
import
Java Error - Java BeginnersJava Error E:\SUSHANT\Core Java\
FileHandling\Error>javac demo1.java
demo1.java:15: possible loss of precision
found : int
required: char...);
//System.out.println("Enter characters, '
q' to quit.");
try
Java read file
package
FileHandling;
import java.io.BufferedReader;
import