-2

I have a problem with writing to file in Java. I want to write to .txt file but when program writes anything , it deletes before written. I don't want it. I used flw.write before using flw.append but nothing change. Can you help me ?

    public void list(String path) throws IOException {

            FileWriter flw=new FileWriter(path);

    flw.append("----------------------------------------------List---------------------------------------------\n");

            for (int i = 0; i < athCtr - 1; i++) {
        flw.append("Author:" + athr[i].getId() + "\t"
                + athr[i].getName() + "\t" + athr[i].getUniv() + "\t"
                + athr[i].getDepart() + "\t" + athr[i].getEmail()+"\n");

        if (athr[i].getArtCtr() != 0) {
            for (int j = 0; j < athr[i].getArtCtr(); j++) {
                flw.append("+" + athr[i].getArticle(j) + ":");
                for (int k = 0; k < artCtr; k++) {
                    if (art[k].getPaperId().equals(athr[i].getArticle(j))) {
                        flw.append("\t" + art[k].getName() + "\t"
                                + art[k].getPublisherName() + "\t"
                                + art[k].getPublishYear()+"\n");


                    }
                }

            }
            flw.append("\n");
        }
    }

    flw.append("Author:" + athr[athCtr - 1].getId() + "\t"
            + athr[athCtr - 1].getName() + "\t"
            + athr[athCtr - 1].getUniv() + "\t"
            + athr[athCtr - 1].getDepart() + "\t"
            + athr[athCtr - 1].getEmail()+"\n");
    if (athr[athCtr - 1].getArtCtr() != 0) {
        for (int j = 0; j < athr[athCtr - 1].getArtCtr(); j++) {
            flw.append("+" + athr[athCtr - 1].getArticle(j) + ":");
            for (int k = 0; k < artCtr; k++) {
                if (art[k].getPaperId().equals(
                        athr[athCtr - 1].getArticle(j))) {
                    flw.append("\t" + art[k].getName() + "\t"
                            + art[k].getPublisherName() + "\t"
                            + art[k].getPublishYear()+"\n");

                }
            }
        }
    }
    flw.append("----------------------------------------------End----------------------------------------------\n\n");
    flw.close();
}
Suheyb
  • 31
  • 1
  • 6

1 Answers1

6

This question should probably be closed as it has been asked and answered many many times, but the key is to use the correct FileWriter constructor, one that takes true as a second parameter. The second true parameter tells Java to open the file for appending rather than overwriting.

//FileWriter flw = new FileWriter(path); // not this
FileWriter flw = new FileWriter(path, true);  // but this

And in fact, I am voting to close this question as a duplicate.

As an aside: you'll probably not want to write directly with the FileWriter but rather will want to wrap it in a BufferedWriter or (my preference) a PrintWriter instance. This will allow you to simply and more efficiently write to the file using println(...) and other familiar methods.

Edit 2: After looking more at your code, I think that actually you should consider using a PrintWriter class and then writing with the printf(...) method as this allows for formatted String output, something that works much better than a bunch of tabs as you're trying to do.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • @Suheyb: You're welcome. After looking more at your code, I think that actually you should consider using a PrintWriter class and then writing with the `printf(...)` method as this allows for formatted String output, something that works much better than a bunch of tabs as you're trying to do. – Hovercraft Full Of Eels Apr 23 '13 at 18:21
  • I fixed this problem using BufferedWriter class and newLine() method. – Suheyb Apr 23 '13 at 18:27
  • @Suheyb: seriously, look at PrintWriter and using its `printf(...)` method. You will be glad you did. – Hovercraft Full Of Eels Apr 23 '13 at 18:28