aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes File.delete() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "File.delete()" Watch "File.delete()" New topic
Author

File.delete()

Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Folks :
I have this question.
File.delete() , actually deletes the file object
Is there a method to actually delete the created file from
OS directory structure?

OR i am asking too much from java to do since it may violate
the security features

Thankx in advance
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Ragu,
The quickest way to find the answer is to check the JDK API
Here's what is says for the delete() method in File.


public boolean delete()
Deletes the file or directory denoted by this abstract
pathname. If this pathname denotes a directory, then the
directory must be empty in order to be deleted.
Returns:
true if and only if the file or directory is
successfully deleted; false otherwise


Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform

[This message has been edited by Jane Griscti (edited July 20, 2001).]


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
I think this will be my new mantra:
When in doubt, check the API.
If you are still in doubt, check the source.
If doubt still remains, ask at JavaRanch.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Thankx for your response.

But my issue is...
File f = new File("a.txt");
FileOutputStream fos = new FileOutputStream(f);
f.delete();

This f.delete() does remove the content of the file f. However
it does not remove the file as such physically (ie a.txt) from the OS directory structure. Is there any issues you folks can think of ... that i need to check to make this happen?
Please help.....
Thankx :-)
Nisheeth Kaushal
Ranch Hand

Joined: Jul 20, 2001
Posts: 87
Well i think that the file will be deleted from ur directory,
after that b'coz if use
boolean exists()
method which particularly check that whether that file exists on ur system or not it returns false.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Ragu,
File.delete() will remove the file from the directory. The problem is, you're calling FileOutputStream with the same File descriptor; which creates an empty file with the same name.
To see the behaviour. Create a test file using notepad. Call File.delete() on the test file. Check your directory; the test file will be gone. Now add a FileOutputStream statement using the same File descriptor; you'll see it the file in your directory with 0 bytes.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File.delete()
 
Similar Threads
How to delete a file in an app client
how to delete contents of sdcard?/
Issue in Deleting File in Java
Deleteing a file
Phasing problem with File.delete()