Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Unable to delete file in button event

  1. #1
    Junior Member
    Join Date
    Mar 2021
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Unable to delete file in button event

    Hi!

    I want to delete file button click, but file is not deleting.

    I tried to delete in main code & code is working.

     file = new File(pathFileLocation);
            if (file.exists()){
                System.out.println("file found");
     
                if (file.delete()){
                    System.out.println("del");
                }else{
                    System.out.println("not del");
                }
     
            }

    how can i delete file on button event ?

    need help.

    Thanks

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Unable to delete file in button event

    I don't know why that code would work differently depending on where it was executed.
    Is the file in the same folder for both cases?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2021
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Unable to delete file in button event

    yes in same folder.

    when file delete failed then i was surprised.

    Then i add same code in start up & file deleted.

    I again put file in same location & tried same code in button event & find file is not deleting.

    --- Update ---

    problem found, in start up i was reading that file but forget to close.

    as i add file.close()

    every thing works perfect.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Unable to delete file in button event

    forget to close.
    Thanks for sharing the solution.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to set a button event trigger?
    By hooshdar3 in forum AWT / Java Swing
    Replies: 28
    Last Post: July 18th, 2014, 11:48 AM
  2. [JTable] Button in table to delete row
    By djosimd in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 16th, 2014, 04:15 PM
  3. Button event handler from another class
    By Scorliss in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 6th, 2013, 06:41 PM
  4. Problem netbeans gui button event handling
    By hiepa in forum AWT / Java Swing
    Replies: 7
    Last Post: December 3rd, 2012, 05:24 PM
  5. this is code for delete button,is there any errors??,plz help guys
    By live89 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 20th, 2012, 12:56 PM