0

I have been working with Files in Java. And I know the basics of reading and writing to/from files. Below is the code that I tried to write

    void qlm(String option,String initiate,String ii,String file_path,String source,List destination){ //,String paths,String src){
    String [] Ln = {"B","C","D"};
    int count =1, counter=1,seq=1;
    try{
        System.out.println("Here: " +file_path);
        PrintWriter pwr = new PrintWriter(new FileWriter(getHandleB()),true);
        for(int i=0;i<Ln.length;i++){
            pwr.println("Sequence_Number" + "|" + "QLM_Operation" + "|" + "II_D" + "|" + "Val_D" + "|" + "List" + "|" + "Type" + "|" + "Status" + "|" + "Source" + "|" + "Destination");
            pwr.println(count + "|" + option + "|" + "DataK" + "|" + "Value" + "|" + Ln + "|" + "Null" + "|" + "Pending" + "|" + source + "|" + Ln[i]);
            count++;
         }
        pwr.close();

getHandleB() is the path of the File. This is performed in the method qlm(parameters)

Now I want to write in the same File (path: getHandleB()) from a different method named handle(parameters)

The output of this function, should write in the same file without removing the contents of the previous method. When i try to write in the file, it removes the previous contents and writes the new one. How can I avoid this. I want all the contents from all the methods to be written. Thanks for all the help.

Mohammed Irfan
  • 93
  • 2
  • 2
  • 11

2 Answers2

2

You are not appending to the File. Use the FileWriter constructor that allows for appending, that has a boolean/true as its second parameter.

PrintWriter pwr = new PrintWriter(new FileWriter(getHandleB(), true),true);

Edit
Separating out the constructor calls in my code above should help you to understand what's going:

FileWriter fileWriter = new FileWriter(getHandleB(), true);
PrintWriter pwr = new PrintWriter(fileWriter, true);

So you see that yes, there are two boolean parameters being used here, but they're being used with different constructors.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • You were faster than me. That happens for searching the links before posting the answer: http://docs.oracle.com/javase/6/docs/api/java/io/FileWriter.html#FileWriter%28java.io.File,%20boolean%29 – Luiggi Mendoza Jun 23 '13 at 15:55
  • Regardless, this question should be and will be closed as a duplicate. – Hovercraft Full Of Eels Jun 23 '13 at 15:55
  • @HovercraftFullOfEels I have used the boolean/true as the second parameter. You can find the Code above. I am trying to write to the same file without removing from different functions. – Mohammed Irfan Jun 23 '13 at 16:00
  • @MohammedIrfan: your code had a boolean, but not in the `FileWriter` constructor. Please look again carefully at the constructor call: `new FileWriter(getHandleB())`. This is one reason to avoid over-use of nesting your code. Consider calling your constructors on separate lines and you'll see it more clearly. – Hovercraft Full Of Eels Jun 23 '13 at 16:01
  • 1
    @MohammedIrfan you did it for the `PintWriter` but not for the `FileWriter` which is noted in the possible duplicated questions and here and in the link I've provided in my first comment on this answer. – Luiggi Mendoza Jun 23 '13 at 16:01
  • @HovercraftFullOfEels one Last question., as I am a little confused. Why are there Two boolean parameters. Isn't it that we need to have only one? – Mohammed Irfan Jun 23 '13 at 16:36
  • @MohammedIrfan: again separate your constructor calls and you'll see which parameter goes with which constructor. – Hovercraft Full Of Eels Jun 23 '13 at 16:39
  • @MohammedIrfan: please see edit to answer for details. – Hovercraft Full Of Eels Jun 23 '13 at 17:02
  • @HovercraftFullOfEels I was trying to understand your previous comment. I got it clear now. Thanks a lot. It helped a Lot. Very helpful – Mohammed Irfan Jun 23 '13 at 17:39
  • @MohammedIrfan: glad it's clear. Much luck! – Hovercraft Full Of Eels Jun 23 '13 at 18:54
1

You need to use the appropriate FileWriter constructor with true as the second argument.

By default, a FileWriter truncates the file it opens.

fge
  • 119,121
  • 33
  • 254
  • 329