java 如何使用Java从目录中删除文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5654166/
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
How to delete a file from a directory using Java?
提问by Anu
Can anyone please tell me how to delete a file in a directory after being opened and loaded on to a database?
谁能告诉我如何在打开并加载到数据库后删除目录中的文件?
Here is my code:
这是我的代码:
public static void main(String[] args) throws SQLException{
int Count= 0;
File directory = new File("C://Documents and Settings//welcome//My Documents//Bluetooth Exchange Folder");
directory.deleteOnExit();
File files[] = directory.listFiles();
for(int index = 0; index < files.length; index++){
try {
FileReader inp = new FileReader (files[index]);
BufferedReader buf = new BufferedReader(inp);
String strLine;
try {
while ((strLine = buf.readLine()) != null)
{
System.out.println(strLine);
String[] dbColumnValues = strLine.split("%");
Connect.DoInsertIntoDB(Long.parseLong(dbColumnValues[0]),dbColumnValues[1],dbColumnValues[2], dbColumnValues[3]);
Count++;
System.out.println(Count + " Row(s) are inserted into the Database");
GenHTML.gen();
}
}
But the files are not deleted in the directory. Please can anyone correct the mistake in my code?
但是目录中的文件并没有被删除。请问谁能更正我代码中的错误?
[Currently, I am testing with 3 files in the directory. After each file gets loaded to the datbase, I want each files to get deleted from the directory.]
[目前,我正在测试目录中的 3 个文件。在每个文件加载到数据库后,我希望从目录中删除每个文件。]
Thanks in advance!
提前致谢!
回答by Jonas Kongslund
It is better to be explicit in your code.
最好在代码中明确说明。
File files[] = directory.listFiles();
for(int index = 0; index < files.length; index++){
{
// Process files[index]
// ...
boolean wasDeleted = files[index].delete();
if (!wasDeleted)
{
// Deal with error
}
}
Also, you need to close your file handles when you are done with them
此外,您需要在完成文件句柄后关闭它们
FileReader inp = new FileReader (files[index]);
try
{
// ...
}
finally
{
inp.close();
}
回答by WhiteFang34
The File.delete()
and File.deleteOnExit()
methods will only delete a directory if it's empty. You'll have to delete the files from the directory as you process them (and make sure there are no subdirectories). Alternatively you can use FileUtils.deleteDirectory()
from Apache Commons IOat the end of your processing.
该File.delete()
和File.deleteOnExit()
方法将只删除一个目录,如果它是空的。在处理文件时,您必须从目录中删除这些文件(并确保没有子目录)。或者,您可以在处理结束时FileUtils.deleteDirectory()
从Apache Commons IO中使用。
回答by Winnifred
Maybe what you need to do is to use the dispose() method for the component that opens the file. What could possibly be the situation is that the file is still seen as opened and locked by a component that it had been opened in, so you have to ensure you use the dispose()
method to solve that problem.
也许你需要做的是对打开文件的组件使用 dispose() 方法。可能的情况是该文件仍被视为已打开并被打开它的组件锁定,因此您必须确保使用该dispose()
方法来解决该问题。
回答by Steve Kuo
The double slashes seems suspect. Either use a single backslash, which you need to quote as \\
, or use a single slash /
.
双斜线似乎很可疑。要么使用单个反斜杠(您需要将其引用为 )\\
,要么使用单个反斜杠/
。
Also, you could try using delete()
when then method returns instead of deleteOnExit()
.
此外,您可以尝试使用delete()
when then 方法返回而不是deleteOnExit()
.
回答by David Weiser
回答by gd1
You can't delete a directory, unless it's empty. If the directory is not empty, it is necessary to first recursively delete all files and subdirectories in the directory.
您不能删除目录,除非它是空的。如果目录不为空,需要先递归删除目录下的所有文件和子目录。
So directory.deleteOnExit() won't work in your case.
所以 directory.deleteOnExit() 在你的情况下不起作用。
More, I suggest you to explicitly delete the files, not using deleteOnExit(). It is a dumb function that won't delete the file on exit if all the input/output streams related to the file are not closed. Always close the streams and explicitly delete the files, then the directory.
此外,我建议您明确删除文件,而不是使用 deleteOnExit()。这是一个愚蠢的函数,如果与文件相关的所有输入/输出流都没有关闭,则不会在退出时删除文件。始终关闭流并明确删除文件,然后是目录。