删除java中的临时文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1026479/
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
delete temporary file in java
提问by
I'm creating temporary file in java but i'm unable to delete it. This is the code I have written:
我正在用 java 创建临时文件,但我无法删除它。这是我写的代码:
temp = File.createTempFile("temp", ".txt");
temp.deleteOnExit();
fileoutput = new FileWriter(temp);
buffout = new BufferedWriter(fileoutput);
回答by Mnementh
Add the following code (after you have done your operations with the file):
添加以下代码(在对文件完成操作之后):
buffout.close();
fileoutput.close();
temp.delete();
As long as some stream on the file is open, it is locked (at least on the windows-implementation of the JVM). So it cannot be deleted.
只要文件上的某个流打开,它就会被锁定(至少在 JVM 的 Windows 实现上)。所以无法删除。
It is good practice always to check if all opened streams get closed again after usage, because this is a bad memory-leak-situation. Your application can even eat up all available file-handles, that can lead to an unusable system.
最好总是检查所有打开的流在使用后是否再次关闭,因为这是一种糟糕的内存泄漏情况。您的应用程序甚至可以吃掉所有可用的文件句柄,这可能导致系统无法使用。
回答by Yoni Roit
There's a bugsaying that if the file is open by filewriter or anything, it won't be deleted. On windows. Check if you close your file writers.
有一个错误说如果文件被文件编写器或任何东西打开,它不会被删除。在窗户上。检查您是否关闭了文件编写器。
Another workaround would be installing a ShutdownHookwhich would manually delete the file.
另一种解决方法是安装ShutdownHook,它会手动删除文件。
回答by oxbow_lakes
You have to shut down a VM cleanlyin order for the deleteOnExit
to work properly (I suspect). On UNIX
a kill
would be a clean shutdown (i.e. the ShutdownHooks
would be processed) whereas a kill -9
would be more like a force quit.
你必须关闭VM干净,以使deleteOnExit
正常工作(我怀疑)。在UNIX
akill
上将是干净的关闭(ShutdownHooks
即将被处理)而 akill -9
将更像是强制退出。
deleteOnExit
definitely works for me!
deleteOnExit
绝对适合我!
回答by vineetawaw kkklll
Code to close the inpustream and outputstream:
关闭输入流和输出流的代码:
FileInputStream in = new FileInputStream();
ArrayList list_in = new ArrayList<FileInputStream>();
list_in.add(in);
FileOutputStream out = new FileOutputStream();
ArrayList list_out = new ArrayList<OutputputStream>();
list_in.add(out);
public do_before_exit()
{
for(int i=0;i<list_in.size();i++)
{
FileInputStream in=(FileInputStream)list_in.get(i)
FileInputStream out=(FileInputStream)list_out.get(i)
in.close()
out.close();
}
}
回答by Bhavesh Padharia
import java.io.File;
import java.io.IOException;
public class TemporaryFileExample
{
public static void main(String[] args)
{
File temp;
try
{
temp = File.createTempFile("myTempFile", ".txt");
System.out.println("Temp file created : " + temp.getAbsolutePath());
//temp.delete(); //For deleting immediately
temp.deleteOnExit(); //Delete on runtime exit
System.out.println("Temp file exists : " + temp.exists());
} catch (IOException e)
{
e.printStackTrace();
}
}
}