File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File.delete()

 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic