File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Deleting a file. HELP! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Deleting a file. HELP!" Watch "Deleting a file. HELP!" New topic

Deleting a file. HELP!

Ben Wong

Joined: Mar 27, 2006
Posts: 19

i am still trying to get a JAR version of my program fully functioning but im getting problems.

For all referenced files i use the class path from class.getResource(). however, for some reason I cannot delete files from the filesystem. It worked before but not anymore! this is the code im using:

please help!

Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

The getResource method returns you a URL object, so the code you have posted should not compile. At any rate, assuming your real code looks more likethen -- also assuming the resource is actually in the file system and not inside the jar -- then I think calling its getPath() method should give you the file name. A little experimenting with System.out.println should show you what you are dealing with.
[ April 26, 2006: Message edited by: Paul Clapham ]
Ben Wong

Joined: Mar 27, 2006
Posts: 19
yeah sorry about that. In my real code i wrote:

The problem I've notice and completely confused by is that when i call a f.exists() it returns false, even though i know it definitely does exist! ive been using loads of System.out.println() lines to try to find out whats going on.

when i change the code and use File f = new File("folder/thefile.xml"); it works fine. but the code then reads the file from OUTSIDE of the JAR, even though the file and folder exist inside the JAR.

im totally confused! please help!
[ April 26, 2006: Message edited by: Ben Wong ]
Henry Wong

Joined: Sep 28, 2004
Posts: 20517

I don't think the toString() method returns what you want. If memory serves, it should be a string representation of the URL -- and not a file path and name.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
I agree. Here's the link:
subject: Deleting a file. HELP!
It's not a secret anymore!