• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

File.delete()

 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 :-)
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Whose rules are you playing by? This tiny ad doesn't respect those rules:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic