Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

read File and write in to file without erasing

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?


import java.io.*;
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

  • 4
2 Solutions
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
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
Element_TAuthor Commented:
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?
You still need to read both files line by line to merge them into one using the same way =)
Why is he closing it?

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now