954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

appending two java text files

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??

mohamad11
Newbie Poster
12 posts since May 2006
Reputation Points: 10
Solved Threads: 0
 

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

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 
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???

mohamad11
Newbie Poster
12 posts since May 2006
Reputation Points: 10
Solved Threads: 0
 

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

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You