Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7368415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:40:11+00:00 2026-05-29T03:40:11+00:00

I was messing around with the java.io.File methods today, and stumbled on something kind

  • 0

I was messing around with the java.io.File methods today, and stumbled on something kind of weird.

When I rename a directory, I can’t seem to ever delete the files or new directory.

    File dir = new File("dir");
    dir.mkdir();

    File file = new File(dir, "afile.txt");
    file.createNewFile();

    File newname = new File(dir, "newName.txt");
    file.renameTo(newname);

    File newdir = new File("newdir");
    dir.renameTo(newdir);

    System.out.println("file exists? " + file.exists());
    System.out.println("file deleted? " + file.delete());
    System.out.println("newname exists? " + newname.exists());
    System.out.println("deleted newname? " + newname.delete());
    System.out.println("dir exists? " + dir.exists());
    System.out.println("dir deleted? " + dir.delete());
    System.out.println("newdir exists? " + newdir.exists());
    System.out.println("deleted newdir? " + newdir.delete());

Yields the output:

    file exists? false
    file deleted? false
    newname exists? false
    deleted newname? false
    dir exists? false
    dir deleted? false
    newdir exists? true
    deleted newdir? false

In the filesystem, though, newdir and newname.txt both still exist!

However, if I comment out the renaming of the directory, the delete works fine.

Are there additional rules between delete() and rename() one needs to know about? There’s not much in the javadoc.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T03:40:12+00:00Added an answer on May 29, 2026 at 3:40 am

    I don’t see any problem with the output:

    1. At the end of the renaming, only newdir/ and newdir/newName.txt exist. Therefore the first six lines of output should be false, and they are. (In particular, the newname variable still refers to dir/newName.txt, which no longer exists.)

    2. newdir does exist; your output confirms this.

    3. The final delete() doesn’t work because the directory is not empty.

    In summary, everything appears to work exactly as one would expect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am messing around with a toy interpreter in Java and I was considering
I've just been messing around with threads in Java to get my head around
I'm messing around with the examples of mt4j multitouch Java library and in the
Possible Duplicate: How can I lock a file using java (if possible) I have
I have been messing around with recursion today. Often a programming technique that is
I've recently started messing around with Java sockets and telnet... I want the user
I have been messing around with synchronization in Java and it has yet to
Messing around with 'extension functions' for the List module. (I spent quite a while
I was messing around with RhinoMocks this morning and couldn't run my tests because
I am messing around with Doctrine (version 1.0.3) to see if would make a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.