appending two java text files

Reply

Join Date: May 2006
Posts: 12
Reputation: mohamad11 is an unknown quantity at this point 
Solved Threads: 0
mohamad11 mohamad11 is offline Offline
Newbie Poster

appending two java text files

 
0
  #1
May 1st, 2006
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:
  1. import java.io.*;
  2. public class append{
  3. public static void main(String[] args) throws IOException {
  4. FileInputStream Message_File=new FileInputStream ("C:/JBuilder4/lib/input6.txt");
  5. BufferedReader buff=new BufferedReader(new InputStreamReader(Message_File));
  6. String Input_Message=new String();
  7. System.out.println("Your input message is as following:");
  8. int c;
  9. while ((c = buff.read()) != -1){
  10. FileOutputStream out = new FileOutputStream("C:/JBuilder4/lib/output6.txt", true);
  11. buff.close();
  12. out.close();
  13. }}
  14. }
it gives no errors but it doesnt append the two files plzz any good code to do so??
Last edited by cscgal; May 1st, 2006 at 9:47 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: appending two java text files

 
0
  #2
May 1st, 2006
You aren't appending to the file anywhere. You create the OutputStream with the proper second parameter, but not writing to it.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 12
Reputation: mohamad11 is an unknown quantity at this point 
Solved Threads: 0
mohamad11 mohamad11 is offline Offline
Newbie Poster

Re: appending two java text files

 
0
  #3
May 1st, 2006
Originally Posted by server_crash
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:
  1. import java.io.*;
  2.  
  3. public class append_files {
  4. public static void main(String[] args) throws IOException {
  5. try {
  6. // create writer for file to append to
  7. BufferedWriter out = new BufferedWriter(
  8. new FileWriter("C:/JBuilder4/lib/output6.txt", true));
  9. // create reader for file to append from
  10. BufferedReader in = new BufferedReader(new FileReader("C:/JBuilder4/lib/output4.txt"));
  11. String str;
  12. while ((str = in.readLine()) != null) {
  13. out.write(str);
  14. }
  15. in.close();
  16. out.close();
  17.  
  18.  
  19. } catch (IOException e) {
  20. } }}
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???
Last edited by cscgal; May 1st, 2006 at 9:48 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: appending two java text files

 
0
  #4
May 2nd, 2006
Don't use Writers and Readers. They work ONLY on line input, which means you'll always get a line terminator.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC