File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes New Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "New Problem" Watch "New Problem" New topic
Author

New Problem

Mohammad Ahmad
Greenhorn

Joined: Mar 21, 2006
Posts: 18
Hi Plz how can i apend two files in jave if i have Two text files and i want to append one to the another into new file
Plz replyyy
thanks in advance
Ramen Chatterjee
Ranch Hand

Joined: Apr 27, 2006
Posts: 62
Hi

Read the file to be appended using a FileInputStream and store the bytes in an array. Then open a FileOutputStream on the file to be appended to, using the constructor, setting append to 'true'. Use the FileOutputStream to write the data in the array.

Hope this helps

Ramen


Could try harder
Mohammad Ahmad
Greenhorn

Joined: Mar 21, 2006
Posts: 18
Thanks alot bur realy i didnt understand well what you say ,can you illustrate more than this???
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Reader,
I'll try explaining what Ramen said in pseudocode:
  • Open a FileOutputStream on the file where you want the data to be appended.
  • Be sure to use this version of the constructor:

  • FileOutputStream(File file, boolean append)
    Note: Setting "append" to true will cause any data written to this stream to the end of the file.
  • Open a FileInputStream on the file that has to be appended.
  • While reading data from input stream, keep appending it to the output stream.
  • Close all the streams.


  • With Best Regards,
    Shyam Prasad Murarka
    Mohammad Ahmad
    Greenhorn

    Joined: Mar 21, 2006
    Posts: 18
    ok i understood what you mean but why when i write this:
    FileOutputStream(File output11,boolean true);
    i got an error cant start the aplication expected ')' at this line even if it does not need ???
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38509
        
      23
    Are you getting a compiler error? Are you aware of this website with a list of compiler errors? You do realise that the true error can be earlier than where the compiler says it is?

    When you write FileOutputStream(File output1, boolean true); you ought to leave out the "File" and the "boolean;" those parameter types go in the method signature, not the method calls.

    Actually there is an easier way in J5.0. Find out about the java.util.Scanner class. Use that to read every line in your first file.
    Set up a 2nd Scanner to read every line in your second file.
    After every line read, use the opposite of a Scanner (which is called a Formatter) to write the line to your File.
    Beware: you cannot pass a "true" to your Formatter, so you have to use the same Formatter throughout the whole operation.

    CR
    Rusty Shackleford
    Ranch Hand

    Joined: Jan 03, 2006
    Posts: 490
    is this the code you are using exactly: FileOutputStream(File output11,boolean true); ?

    You do not declare references in the arguments like that, ever. And of course you need to create a FileOutputStream object.

    Somthing like this would work:

    FileOutputStream out = FileOutputStream(new File("filename.txt"),true);

    or
    File output11 = new File("filename.txt"):

    FileOutputStream out = FileOutputStream(output11. true);

    or

    FileOutputStream out = FileOutputStream("filename.txt", true);

    The suggestion to use Scanner is not a bad one, but be aware that it is not nearly as useful or flexible as the java.io classes. As the description says: "A simple text scanner which can parse primitive types and strings using regular expressions."

    In other words, you will have to learn java.io sooner or later.
    [ April 29, 2006: Message edited by: Rusty Shackleford ]

    "Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
    Mohammad Ahmad
    Greenhorn

    Joined: Mar 21, 2006
    Posts: 18
    Hi I tried the second solutions which is:
    FileOutputStream out = FileOutputStream("output4.txt", true);
    and i tried all what you told me about but it gives me this error:
    "Append_Message.java": Error #: 300 : method FileOutputStream(java.lang.String, boolean) not found in class Append_Extract.Append_Message at line 57, column 24
    i have imprted java:
    import java.*;
    so wut is the problem here???
    Henry Wong
    author
    Sheriff

    Joined: Sep 28, 2004
    Posts: 18765
        
      40

    Hi I tried the second solutions which is:
    FileOutputStream out = FileOutputStream("output4.txt", true);


    The correct syntax is...



    BTW, I find it mildly interesting that that title of this thread is "new problem"...

    Henry
    [ April 30, 2006: Message edited by: Henry Wong ]

    Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
    Mohammad Ahmad
    Greenhorn

    Joined: Mar 21, 2006
    Posts: 18
    thanks Henry but i wrote the following code:
    try {
    FileInputStream Message_File=new FileInputStream ("C:/JBuilder4/lib/output5.txt");
    BufferedReader buff=new BufferedReader(new InputStreamReader(Message_File));
    String Input_Message=new String();
    while((Input_Message=buff.readLine() )!=null){
    FileOutputStream out = new FileOutputStream("C:/JBuilder4/lib/output6.txt", true);
    }
    } catch (IOException ioe) {
    ioe.printStackTrace();}
    }}


    There was no problem with it or errors but when i opened the file output6.txt i didint find the other file appended to the end of this file
    so whyyyy
    Henry Wong
    author
    Sheriff

    Joined: Sep 28, 2004
    Posts: 18765
        
      40

    There was no problem with it or errors but when i opened the file output6.txt i didint find the other file appended to the end of this file
    so whyyyy


    You need to do something to actually write the lines that you read. Basically, you need to do for the write side, something similar that you did for the read side.

    Java Doc is your friend. Look up BufferedWriter, OutputStreamWriter, and/or FileWriter, to configure the matching stream that you did for the read side.

    Henry
    [ April 30, 2006: Message edited by: Henry Wong ]
    Mohammad Ahmad
    Greenhorn

    Joined: Mar 21, 2006
    Posts: 18
    plz i do the following but no new thing happens i want the file to be appended ..

    try {
    true));
    FileInputStream Message_File=new FileInputStream ("C:/JBuilder4/lib/output4.txt");
    BufferedReader buff=new BufferedReader(new InputStreamReader(Message_File));
    String Input_Message=new String();
    BufferedWriter outfile =
    new BufferedWriter(new FileWriter("C:/JBuilder4/lib/output6.txt "));
    outfile.write(Input_Message);
    outfile.close();
    while((Input_Message=buff.readLine() )!=null){
    FileOutputStream out = new FileOutputStream("C:/JBuilder4/lib/output6.txt", true);
    out.close();

    }
    }catch (IOException e) {
    System.out.println(e.getMessage());
    }}
    Henry Wong
    author
    Sheriff

    Joined: Sep 28, 2004
    Posts: 18765
        
      40

    plz i do the following but no new thing happens i want the file to be appended ..


    hmmmmm... not trying to offend... But do you even know how I/O works? It looks like you are just randomly pasting in code without know how anything works.

    This is obviously a homework problem, so I can't give you the answer. But at the same time, I can't seem to give you hints either -- as you seem to just take the first thing you find based on the hints and paste it in.

    Anyway, I really recommend that you go back to your text, and review the section on I/O. You really need a basic understanding of the java I/O streams first.

    Henry
    Rusty Shackleford
    Ranch Hand

    Joined: Jan 03, 2006
    Posts: 490
    I apologize for the mistyping, but Henry Wongs last post is spot on. Re-read the basics, and try again.
    Mohammad Ahmad
    Greenhorn

    Joined: Mar 21, 2006
    Posts: 18
    Thanks for all your comments ,but that doesnt mean that iam like wut u said .i really can understand I/O straems well but the problem is the same so wut is the problem in my code??
    import java.io.*;

    public class CopyBytes {
    public static void main(String[] args) throws IOException {

    FileInputStream Message_File=new FileInputStream ("C:/JBuilder4/lib/output4.txt");
    BufferedReader buff=new BufferedReader(new InputStreamReader(Message_File));
    String Input_Message=new String();
    System.out.println("Your input message is as following:");
    while((Input_Message=buff.readLine() )!=null){
    FileWriter out = new FileWriter("C:/JBuilder4/lib/output6.txt" , true);
    out.close();


    }}}
    if no body want to help me also no problem but that doesnt mean that i didint tried well in ...
    Thanks for all
    Garrett Rowe
    Ranch Hand

    Joined: Jan 17, 2006
    Posts: 1296
    You're intstantiating a new FileWriter instance but the only method you call on it is close(). Hint: if you look at the documentation for FileWriter you will see that it has a number of methods called write().

    It may help if you take a look at the IO tutorial also.


    Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
    Mohammad Ahmad
    Greenhorn

    Joined: Mar 21, 2006
    Posts: 18
    ok i got it and finally i wrote this code:
    import java.io.*;

    public class CopyBytes {
    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???
    Yuriy Zilbergleyt
    Ranch Hand

    Joined: Dec 13, 2004
    Posts: 429
    Originally posted by Mohammad Ahmad:
    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???


    Just in case, have you checked your first file to make sure that there isn't an empty line after Mohammad? If you haven't, open it with notepad and hold the down key until you're at the very bottom, and then the end key to go to the end of line. If the cursor stops below what you intend the last line to be, you have to backspace to the last real character.

    Yuriy
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: New Problem