This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

New Problem

 
Mohammad Ahmad
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mohammad Ahmad
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot bur realy i didnt understand well what you say ,can you illustrate more than this???
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

  •  
    Mohammad Ahmad
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 48407
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 490
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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 ]
     
    Mohammad Ahmad
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Marshal
    Pie
    Posts: 20892
    75
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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 ]
     
    Mohammad Ahmad
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Marshal
    Pie
    Posts: 20892
    75
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Marshal
    Pie
    Posts: 20892
    75
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 490
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I apologize for the mistyping, but Henry Wongs last post is spot on. Re-read the basics, and try again.
     
    Mohammad Ahmad
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 1296
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Mohammad Ahmad
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 429
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic