このメソッドは特定の処理を終えた後にworkフォルダ内のファイルを全て削除する処理を記述した
メソッドですが、実行しても削除できないファイルが存在します。(削除できないファイルは実行するたびに異なります)
まれに全てのファイルが削除されることもありますが
大抵の場合は、ファイルに消し残しがあります。
どのように改善すればファイルを全て削除できるようになるのでしょうか。
Java
1public class test { 2 3 static String header=""; 4 static String torer=""; 5 static String HenkanName = ""; 6 static String[] date=new String[9998]; 7 static String[] headersyu=new String[999998]; 8 static String[] torersyu=new String[999998]; 9 static int num=0; 10 static int headerNum=2; 11 static int torerNum=2; 12 static int n=0; 13 static int headerkotei=9; 14 static BufferedWriter sinkibuffer; 15 16 public static void test(int head, String name) { 17 headerkotei = head; 18 19 File filelist = new File(".\work"); 20 File files[] = filelist.listFiles(); 21 int a = 1; 22 for(int i = 0; i < files.length; i ++) { 23 if(files[i].getName().contains("分割")) { 24 header=""; 25 torer=""; 26 HenkanName = ""; 27 date=new String[9998]; 28 headersyu=new String[999998]; 29 torersyu=new String[999998]; 30 num=0; 31 headerNum=2; 32 torerNum=2; 33 n=0; 34 Date now = new Date(); 35 SimpleDateFormat f = new SimpleDateFormat("yyyyMMddHHmmss"); 36 String s = f.format(now); 37 HenkanName = name + "\" + s + "_復元" + a + ".csv"; 38 a ++; 39 try { 40 FileInputStream input = new FileInputStream(files[i]); 41 InputStreamReader stream = new InputStreamReader(input,"SJIS"); 42 BufferedReader buffer = new BufferedReader(stream); 43 String yomikomi; 44 CsvSakusei(0); 45 while ((yomikomi = buffer.readLine()) != null) { 46 if(header.equals("")||!(yomikomi.substring(0, headerNum-1).equals(header))) { 47 if(!(header.equals(""))) { 48 CsvSakusei(1); 49 } 50 headerkubun(yomikomi); 51 torerkubun(yomikomi); 52 } 53 datekubun(yomikomi); 54 } 55 CsvSakusei(2); 56 Date now2 = new Date(); 57 SimpleDateFormat f2 = new SimpleDateFormat("yyyyMMddHHmmss"); 58 String s2 = f.format(now2); 59 input.close(); 60 stream.close(); 61 buffer.close(); 62 files[i].delete(); 63 } 64 catch (IOException ex) { 65 ex.printStackTrace(); 66 } 67 } 68 } 69 for(int i = 0; i < files.length; i ++) { 70 files[i].delete(); 71 } 72 }
