How to delete a file from a directory using Java?
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.]
开发者_如何转开发Thanks in advance!
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();
}
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 IO at the end of your processing.
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()
.
According to the API:
Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates.
In your code, however, you are treating that function as if it immediately deletes the directory.
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.
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.
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.
精彩评论