deleteOnExit不删除文件

时间:2014-07-15 12:43:09

标签: java

我创建了一些临时使用的文件,并将它们用作某些方法的输入。我打电话给

deleteOnExit() 

我创建的所有文件。但仍有一个档案。

我认为这是因为该文件仍在使用中,但编译器只在当前行完成后才转到下一行?(单线程)

虽然实际上由于java覆盖不是问题,但总是只有一个文件。我想了解它为什么会发生,如果我可以使用

 Thread.sleep(sometime);

编辑: -

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

创建所有文件(5)后,我刚添加了这一行

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

删除除最后一个文件以外的所有文件。

2 个答案:

答案 0 :(得分:17)

确保写入文件的所有流都已关闭。如果流未关闭,则文件将被锁定,删除将返回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 :(得分: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.
        }
    }

}