read File and write in to file without erasing

Posted on 2009-12-26
Last Modified: 2013-11-23
Now I can read the file with a java code, how I can write in to file without erasing anything?

The text which is in the file before have to be untasted.

But my code delete the text which is written before.

Do anyone has an idea how I can read  two files and put it in to one or in to new one?


public class LiesDateiEin {

     * @param args the command line arguments
    public static void main(String[] args) {
        // Dateiname wir übergeben

        String filenameIn = args [0];

        try {

             FileInputStream fis = new FileInputStream (filenameIn);
             InputStreamReader isr = new InputStreamReader(fis);
             BufferedReader bur = new BufferedReader (isr);

             // die erste Zeile wird eingelesen

             String sLine = bur.readLine();

             // lies alle Zeilen aus, bis keine mehr vorhanden sind
             // und gib sie nacheinander aus
             //falls von vornherein nichts in der Datei enthalten
             // ist, wird dieser Prorammabschiniit übersprungen

             int zaehler = 0;

             while (sLine !=null){
                 System.out.println ("Zeile"+zaehler+":"+sLine);
                 sLine = bur.readLine();
             // schliess die Datei

        }catch (IOException eIO){
            System.out.println ("folgender Fehler trat auf: "+eIO);

        String filenameOutput = args [0];

        try {

            BufferedWriter myWriter = new BufferedWriter(new FileWriter(filenameOutput, false));

            //schreibe zeilenweise in die Datei filename Output

            myWriter.write("Hans Mueller \n");
            myWriter.write("Gundel Gaukel \n");
            myWriter.write("Fred Feuermacher\n");

        catch (IOException eIO){
            System.out.println("Folgender Fehler trag auf:"+ eIO);



Open in new window

Question by:Element_T
    LVL 5

    Assisted Solution

    Can you post your code?

    For example, if you are using FileOutputStream there is a constructor where you can specify if you want to append to your file instead of overwriting it

    FileOutputStream(String filePath, boolean append)

    Hope this helps
    LVL 5

    Expert Comment

    Sorry, my mistake about posting your code.

    You are using FileWriter which has a FileWriter(File file, boolean append) constructor.

    If append = true then bytes will be written to the end of the file rather than the beginning

    Accepted Solution

    BufferedWriter myWriter = new BufferedWriter(new FileWriter(filenameOutput, true));

    than works :-) thanks

    can I use the same way to put two separate file in to one?
    LVL 5

    Expert Comment

    You still need to read both files line by line to merge them into one using the same way =)
    LVL 5

    Expert Comment

    Why is he closing it?

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Suggested Solutions

    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now