1

I can't delete a file in Java. In my program, I create a file, do something with it, and then I have to delete it. When I call myFile.delete(), it returns false. I have checked the path it is trying to delete and its correct, I also have administrator privileges (I'm working on Windows 7). Here is my piece of code, very simple:

File aux = new File(System.getProperty("user.dir")+"//tmp.ps");

CreatePostScript(aux.getAbsoluteFilePath());
SendToPrinter();

try{
    aux.delete();
}
catch(SecurityException ex){
     ex.printStackTrace();
}

Edit, I have read some properties of the File object:

canRead() returns false
canWrite() returns false
exists() returns false
getPath() returns the_actual_path_of_the_file
isFile() returns false
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Joel
  • 193
  • 1
  • 6
  • 12

5 Answers5

2

Are you doing that from inside some IDE? Probably the IDE is holding file handle. Try to download Unlocker and to see who is holding the handle.

Flot2011
  • 4,601
  • 3
  • 44
  • 61
  • Unlocker says no program is locking the file – Joel Nov 29 '12 at 13:06
  • Try to delete the file with Unlocker. If it can be deleted , then definitely something (very probably NetBeans)is holding the handle. Also try to rename the file and then to delete it. If it works, then, again, something (very probably NetBeans)is holding the file handle. – Flot2011 Nov 29 '12 at 13:38
1

As you are performing processing on the file it is likely that an OutputStream is still open. Call out.close(); before attempting to delete the file.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
1

Check that you provide user.dir property thought -D command-line argument. And use File.pathSeparator instead of //

ice
  • 777
  • 1
  • 4
  • 19
  • Thanks for the tip, but I think that is not the problem, the filepath that the getPath() function returns is correct. – Joel Nov 29 '12 at 13:11
  • 1
    Why you add `//` before file name? In windows file separator is `\\\`. – ice Nov 29 '12 at 13:20
1

You might consider using Java 7's NIO2 API for the operation. Instead of returning a success value, it actually throws an exception when something stops it from performing an operation.

Urs Reupke
  • 6,791
  • 3
  • 35
  • 49
0

in your code, i see you are not closing the stream before you are deleting, close the stream and delete the file.

developer
  • 9,116
  • 29
  • 91
  • 150
  • What stream? I just create the File and use its path to execute a command with Runtime.getRuntime().exec – Joel Nov 29 '12 at 13:01
  • you are calling methods after file creation, what does those methods do, – developer Nov 29 '12 at 13:06
  • The CreatePostScript() method does call the exec() function passing the filepath as an argument. The method itself don't do any work with the file, I use the File class to work with filepaths – Joel Nov 29 '12 at 13:09
  • if it doesnot do any thing with that file, whay are you passing it to the method? – developer Nov 29 '12 at 13:15
  • Sorry, that was a mistake, it actually is CreatePostScript(aux.getAbsolutePath()). Again, sorry – Joel Nov 29 '12 at 13:17
  • please clarify, are you closing that exec() call properly. i feel, they may stops in deleting the file, please provide more details about that method – developer Nov 29 '12 at 13:20
  • I think the problem is the exec() call. It calls a .exe to create a PostScript file, but the delete() call is just after exec(), so it doesn't wait until the .exe finishes it's work. – Joel Nov 29 '12 at 13:25
  • that what i said in previous comment, check with that call – developer Nov 29 '12 at 13:26