Java deleteOnExit 不删除文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24758520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
deleteOnExit not deleting file
提问by raka
I have created a few files for temporary use and used them as inputs for some methods. And I called
我创建了一些临时使用的文件,并将它们用作某些方法的输入。我打电话给
deleteOnExit()
on all files I created. But one file still remains.
在我创建的所有文件上。但是一个文件仍然存在。
I assume it is because the file is still in use, but doesn't the compiler go to next line only after the current line is done?(Single thread)
我认为这是因为文件仍在使用中,但是编译器不是只有在当前行完成后才转到下一行吗?(单线程)
While its not a problem practically because of java overwrite, there is only one file always. I would like to understand why it happens and also if I can use
虽然实际上由于 java 覆盖它不是问题,但始终只有一个文件。我想了解为什么会发生这种情况以及我是否可以使用
Thread.sleep(sometime);
Edit:-
编辑:-
File x = new file("x.txt");
new class1().method1();
After creating all files(5), I just added this line
创建所有文件(5)后,我刚刚添加了这一行
x.deleteOnExit(); y.deletOnExit() and so on...
All the files except that last one is deleted.
除最后一个文件外的所有文件都被删除。
采纳答案by Forever Noob
Make sure that whatever streams are writing to the file are closed. If the stream is not closed, file will be locked and delete will return false. That was an issue I had. Hopefully that helps.
确保写入文件的任何流都已关闭。如果流没有关闭,文件将被锁定,删除将返回 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();
}
}
}
}
}
回答by A.W.
In order to close the file that you have opened in your program, try creating an explicit termination method.
为了关闭您在程序中打开的文件,请尝试创建显式终止方法。
Therefore, try writing the following:
因此,尝试编写以下内容:
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.
}
}
}