1

I am trying to write the contents of my ArrayList to a text file which is already created in my G drive ( the path has been mentioned below ). I am using BufferedWriter to write the contents of the Arraylist into the text file

It compiles without any errors but somehow the textfile- sampleOutput.txt is empty. Any ideas why this is so ?

public void WriteOutput( ArrayList<detailTom> tomData1)
{
      try 
      {
          BufferedWriter writer = new BufferedWriter(new FileWriter("G:/sampleOutput.txt"));
          for (detailTom detail : tomData1) 
            {

                writer.write(detail.toString());
            }

      } 

      catch (IOException ex) 
      {
          Logger.getLogger(MyInterface.class.getName()).log(Level.SEVERE, null, ex);
      } 
}
Nidhin_toms
  • 707
  • 4
  • 18
  • 29
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – tomrozb Jan 03 '15 at 14:20

1 Answers1

2

try closing the BufferedWriter object after you finish writing:

    public void WriteOutput( ArrayList<detailTom> tomData1){
      try{
          BufferedWriter writer = new BufferedWriter(new FileWriter("G:/sampleOutput.txt"));
          for (detailTom detail : tomData1) {
  writer.write(detail.toString());
            }
    writer.close();
   } 

      catch (IOException ex) 
      {
          Logger.getLogger(MyInterface.class.getName()).log(Level.SEVERE, null,ex);
      } 
}
Nick
  • 930
  • 1
  • 11
  • 17