java - File.delete() fails to delete files in a directory -


after writing text files directory, trying delete empty files written printwriter.

file.delete() function fails delete file. below code writing , deleting.

private static void writefile(arraylist<arraylist<string>> listrowval, string szoutputdir, arraylist<string> listheader){             printwriter pw  = null;                 try {                                arraylist<string> listcells = listrowval.get(0);                        int icells = listcells.size();                      for(int k=0; k<icells; k++){                string language = listheader.get(k);                string szfilename = "files_"+ language +".csv";                                 pw  = new printwriter(new filewriter(szoutputdir + file.separator + szfilename));                               for(arraylist<string> listncrcellval : listrowval){                    string szval = listncrcellval.get(k);                    if(szval != null && szval.trim().length() > 0){                        pw.println(szval);                    }                    pw.flush();                                }                               }                 } catch (ioexception e) {         // todo auto-generated catch block         e.printstacktrace();     }finally {         try {             if(pw != null){              pw.close();              pw = null;             }             //system.gc();             deleteemptyfiles(szoutputdir);         } catch (exception e) {             // todo auto-generated catch block             e.printstacktrace();         }     }     }  private static void deleteemptyfiles(string szdirpath) {         file file = new file(szdirpath);         if (file.isdirectory()) {             string[] files = file.list();             if (files.length > 0) {                 (string szfilename : files) {                     file deletefile = new file(szdirpath + file.separator + szfilename);                     if (deletefile.length() == 0) {                                              //deletefile.setwritable(true, false);                         boolean bdeleted = deletefile.delete();                                              if(bdeleted){                             system.out.println(deletefile.getname() + " deleted.");                                                  }                     }                  }             }         }     } 

what going wrong..??

you must close each printwriter, i.e. pw.close() must on end of "k" loop.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -