문제

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);
      } 
}
도움이 되었습니까?

해결책

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);
      } 
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top