Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Simple Java Question

  • 03-03-2005 01:21PM
    #1
    Closed Accounts Posts: 43


    Can anyone tell me how to delete a file using Java. I just need it to be same as what windows would if if I was to delete it like that. Probably isnt much to it but I cannt figure it out for myself. If anyone can direct me to sites or code samples that I could play around with. Thanks


Comments

  • Registered Users, Registered Users 2 Posts: 17,727 ✭✭✭✭Sherifu


    File.delete(); i would imagine.

    The easiest is always best!


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html#delete()

    It's not static so you would have to create a class to your File. Eg.
    boolean fileWasDeleted = (new File("filename.txt")).delete();
    
    if (!fileWasDeleted) {
      // Something went wrong.
    }
    

    If you are making an applet it will probably be sandboxed so not sure if it will let you delete stuff.


  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    Hobbes wrote:
    http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html#delete()

    It's not static so you would have to create a class to your File. Eg.
    boolean fileWasDeleted = (new File("filename.txt")).delete();
    
    if (!fileWasDeleted) {
      // Something went wrong.
    }
    

    If you are making an applet it will probably be sandboxed so not sure if it will let you delete stuff.
    Will it not require a try-catch block for creating the File?


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    seamus wrote:
    Will it not require a try-catch block for creating the File?

    What you want me to write everything? :)

    It won't throw an exception unless the SecurityManager exists. You would need try/catch for the general File command I guess though, but it may not always trap the delete.


Advertisement