0

I have the following code, What it does is,it gets the response from http connection and writes it to logs.txt. When i execute the program the response gets written in txt file,but when i run the same program again it doesn't write response to txt file.

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String inputLine;
                StringBuffer response = new StringBuffer();
                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();
                BufferedWriter out = new BufferedWriter(new FileWriter("C:/Users/mkumaru/Desktop/Elastic/elasticsearch/server/logs/logs.txt"));
                try {
                    out.write(response.toString());
                }
                catch (IOException e)
                {
                    System.out.println("Exception ");
                }
                finally
                {
                    out.close();
                }

So what i am expecting is, In the first execution, logs.txt should be, response1

In the second execution of same program, response1 response2

How can i do this.

Radha
  • 61
  • 1
  • 10

1 Answers1

0
BufferedWriter writer = new BufferedWriter(
                            new FileWriter("C:/Users/mkumaru/Desktop/Elastic/elasticsearch/server/logs/logs.txt", true)  //Set true for append mode
                        ); 
writer.newLine();   //Add new line
writer.write(textToAppend);
Monis Majeed
  • 1,358
  • 14
  • 21