Reputation: 1283
I have some trouble deleting a file in Windows 7, using this code :
private static final String WIN_DIR_TEST = "D:"+File.separator+"Users"+File.separator+"u119255"+File.separator+"Desktop"+File.separator;
...
File file = null;
FileWriter fileWriter = null;
String localPath = WIN_DIR_TEST.concat("abc.degno");
file = new File(localPath);
fileWriter = new FileWriter(file, true);
fileWriter.write("qwertyuiop\n");
fileWriter.close();
ftp.send(localPath, distantPath);
file.delete();
And this last line always return false, and I don't understand why. Also, no exception occurs.
Upvotes: 1
Views: 4349
Reputation: 21
I had issues deleted with deleting a folder containing other files.
So I ended up with deleting recursively the folder.
private void deletedFile(File file)
{
if ( file.isFile() )
{
file.delete();
}
else
{
File[] subFiles = file.listFiles();
for ( File subFile : subFiles )
{
deletedFile(subFile);
}
file.delete();
}
}
Upvotes: 0
Reputation: 439
Try using file.deleteOnExit()
instead of file.delete()
If that works maybe there's something in your code that still has a handle on the file.
Upvotes: 1
Reputation: 496
Java has often Problems to write to Files which lies directly in C: Hard Disk (or the Disk where Win is installed). Move the File to a subfolder on C:.
Upvotes: 1
Reputation: 22392
Try to put the exception handling like this:
try {
if (file.delete()) {
System.out.println(file.getName() + " is deleted!");
} else {
System.out.println("Delete operation is failed.");
}
} catch (Exception e) {
e.printStackTrace();
}
Upvotes: 1
Reputation: 589
It is simple, because as I can see from your code the "file" is always equals null :) Where do you initialize you file variable?
Upvotes: 0