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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Simple Java Question

  • 03-03-2005 1: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,190 ✭✭✭✭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