hi to all...
Plz i wnt a valuable code to append two text files in java i tried alot using WriteBuffered and FileOutputStream(file,true) but without any use i wrote the following code:

import java.io.*;
public class append{
public static void main(String[] args) throws IOException {
FileInputStream Message_File=new FileInputStream ("C:/JBuilder4/lib/input6.txt");
BufferedReader buff=new BufferedReader(new InputStreamReader(Message_File));
String Input_Message=new String();
System.out.println("Your input message is as following:");
int c;
while ((c = buff.read()) != -1){
FileOutputStream out = new FileOutputStream("C:/JBuilder4/lib/output6.txt", true);
buff.close();
out.close();
}}
}

it gives no errors but it doesnt append the two files plzz any good code to do so??

Recommended Answers

All 3 Replies

You aren't appending to the file anywhere. You create the OutputStream with the proper second parameter, but not writing to it.

You aren't appending to the file anywhere. You create the OutputStream with the proper second parameter, but not writing to it.

ok i got it and finally i wrote this code:

import java.io.*;
 
public class append_files {
public static void main(String[] args) throws IOException {
try {
// create writer for file to append to
BufferedWriter out = new BufferedWriter(
new FileWriter("C:/JBuilder4/lib/output6.txt", true));
// create reader for file to append from
BufferedReader in = new BufferedReader(new FileReader("C:/JBuilder4/lib/output4.txt"));
String str;
while ((str = in.readLine()) != null) {
out.write(str);
}
in.close();
out.close();
 
 
} catch (IOException e) {
} }}

and it did the appending i need but there still one problem that it appends the second file to the first file ,but each one in separate line for example if the first file conatains "Mohammad" and the second file conatins "Ahmad" ,the Appending output is as following:
Mohammad
Ahmad
but i need this output:
MohammadAhmad
so how can i change my code to do this???

Don't use Writers and Readers. They work ONLY on line input, which means you'll always get a line terminator.

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.