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.

3
  • @BheshGurung I did see that and I was not able to solve my problem. SO i had to post it. Thanks. Commented Jun 23, 2013 at 15:58
  • @MohammedIrfan then next time read carefully the answer and look the difference with your code to avoid posting duplicated questions. Commented Jun 23, 2013 at 15:59
  • -1 for the question. My Bad. I just started using Stack Overflow. :( Commented Jun 23, 2013 at 16:02

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

10 Comments

You were faster than me. That happens for searching the links before posting the answer: docs.oracle.com/javase/6/docs/api/java/io/…
Regardless, this question should be and will be closed as a duplicate.
@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.
@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.
@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.
|
1

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

By default, a FileWriter truncates the file it opens.

1 Comment

1+ great minds think alike.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.