Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Simple Java Question

Options
  • 03-03-2005 2: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 Posts: 17,727 ✭✭✭✭Sherifu


    File.delete(); i would imagine.

    The easiest is always best!


  • Registered Users 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 Posts: 68,317 ✭✭✭✭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 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