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 3 of 3

Thread: FIle Won't Delete

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default FIle Won't Delete

    Hello, I"m trying to delete a text file but to no avail.

    Here is the code
    File myFile = new File("test.txt");
    Scanner input = new Scanner(myFile);
    String oldText = "";
    while(inputOld.hasNextLine())
    {
    oldText = oldText + inputOld.nextLine();
    }
    input.close();
    myFile.delete();

    File is still there when I look. Path is correct, and the weird thing is if I create a new File in the code and point it an existing file without using scanners I can delete it.

    Example is:

    File myFile = new File("test.txt");
    myFile.delete()

    and that works... So my assumption is that because the file is being used by program I cannot delete it. How can I force my program to stop using the file since .close() method appears to not be working...


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: FIle Won't Delete

    First, you should evaluate the returned value by the delete method which tells you if the file has been deleted or not. If this returns true you should make sure you are pointing to the file you think you are pointing to. Second, do you ever you the scanner you created? Third, I'd recommend using a StringBuilder rather than String addition.

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: FIle Won't Delete

    I found the solution, input was not being closed in another method while reading the file. Thanks for the help, it had me confused for a while.

Similar Threads

  1. please need help ... for the delete method in array
    By yanikapausini:) in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 13th, 2012, 02:22 PM
  2. [SOLVED] wildcard delete
    By macko in forum What's Wrong With My Code?
    Replies: 13
    Last Post: November 1st, 2011, 03:31 PM
  3. how to delete a JTextField?
    By A4Andy in forum AWT / Java Swing
    Replies: 10
    Last Post: August 31st, 2011, 11:33 AM
  4. Jar self delete on exit?
    By KiwiProg in forum Java Theory & Questions
    Replies: 1
    Last Post: December 19th, 2010, 01:54 AM
  5. Can I delete one of these two?
    By ice in forum Java IDEs
    Replies: 2
    Last Post: November 14th, 2010, 03:02 AM