Question

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);
      } 
}
Was it helpful?

Solution

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);
      } 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top