2014-07-15 30 views
10

我已經創建了一些臨時使用的文件,並將它們用作某些方法的輸入。我打電話給deleteOnExit不刪除文件

deleteOnExit() 

關於我創建的所有文件。但有一個文件仍然存在。

我想這是因爲該文件仍在使用,但沒有編譯器去當前行完成後才下一行?(單線程)

雖然因爲實際上它不是一個問題java覆蓋,總是隻有一個文件。我想知道爲什麼會發生,也如果我可以使用

Thread.sleep(sometime); 

編輯: -

File x = new file("x.txt"); 
new class1().method1(); 

創建的所有文件(5)後,我只是說此行

x.deleteOnExit(); y.deletOnExit() and so on... 

除最後一個文件以外的所有文件都被刪除。

+2

你會想追加你的代碼。問題可能在其中。 –

+0

您是否真的退出了Java進程? –

+0

處理此問題的一種更好的方法是使用終結器... –

回答

14

確保任何流寫入文件被關閉。如果流未關閉,則文件將被鎖定,並且刪除將返回false。這是我的問題。希望有幫助。

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.StringWriter; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Date; 
import java.util.List; 


public class Test { 

    public static void main(String[] args) { 

     File reportNew = null; 
     File writeToDir = null; 

     BufferedReader br = null; 
     BufferedWriter bw = null; 
     StringWriter sw = null; 

     List<File> fileList = new ArrayList<File>(); 

     SimpleDateFormat ft = new SimpleDateFormat("yyyymmdd_hh_mm_ss_ms"); 


     try { 

      //Read report.new file 
      reportNew = new File("c:\\temp\\report.new"); 

      //Create temp directory for newly created files   
      writeToDir = new File("c:\\temp"); 
      //tempDir.mkdir(); 

      //Separate report.new into many files separated by a token 
      br = new BufferedReader(new FileReader(reportNew)); 
      sw = new StringWriter(); 
      new StringBuilder(); 



      String line; 
      int fileCount = 0; 

      while (true) { 

       line=br.readLine(); 

       if (line == null || line.contains("%PDF")) { 

        if (!sw.toString().isEmpty()) { 

         fileCount++; 

         File _file = new File(writeToDir.getPath() 
              + File.separator 
              + fileCount 
              + "_" 
              + ft.format(new Date()) 
              + ".htm"); 

         _file.deleteOnExit(); 
         fileList.add(_file); 


         bw = new BufferedWriter(new FileWriter(_file)); 
         bw.write(sw.toString()); 

         bw.flush(); 
         bw.close(); 
         sw.getBuffer().setLength(0); 

         System.out.println("File " 
              + _file.getPath() 
              + " exists " 
              + _file.exists()); 
        } 

        if (line == null) 
         break; 
        else 
         continue; 
       } 

       sw.write(line); 
       sw.write(System.getProperty("line.separator")); 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      if (bw != null) { 
       try { 
        bw.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 
} 
+1

用代碼說明你的觀點可能很有用 – beresfordt

2

爲了關閉您在程序中打開的文件,請嘗試創建一個明確的終止方法。

因此,嘗試寫如下:

public class ClassThatUsesFile { 

    private String filename; 
    private BufferReader reader; 

    public ClassThatUsesFile (String afile) { 
     this.filename = afile; 
     this.reader = new BufferReader(new FileReader(afile)); 
    } 

    // try-finally block guarantees execution of termination method 

    protected void terminate() { 
     try { 
      // Do what must be done with your file before it needs to be closed. 
     } finally { 
      // Here is where your explicit termination method should be located. 
      // Close or delete your file and close or delete your buffer reader. 
     } 
    } 

} 
-7

這裏沒問題。 deleteOnExit()對我來說很有魅力。

File file = new File(UUID.randomUUID().toString() + ".html"); 
file.deleteOnExit(); 

// open file here 

// process file here 

// flush/close file here