简体   繁体   中英

java isDirectory showing wrong result

I have the following code that checks if a file is a directory, and if not, creates it.

        File folder = new File(destFolder);
        if (!folder.isDirectory()) {
            if (!folder.mkdir()) {
                System.out.println("Could not create " + folder.getAbsolutePath());
                return null;
            }
        }

The code outputs "Could not create " always. I checked and confirmed that the folder with that name already exists. What could be the reason?

If I remove the destFolder, still it is showing the same error. Checked the permissions, I have read, write and execute permissions to the folder. (Weird thing is, it was working till last Friday!)

My machine runs Windows 7, and I am running the code from Netbeans. Java Platform is - JDK 1.7

You only get to the mkdir() call if the file is not a directory, in other words if it is a file. You're trying to run mkdir() against a file. You should be checking to see if it exists, not if it's a directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM