Java isFile(), isDirectory() without checking for existence

20,770

Solution 1

Sounds like you know what you want, according to your update: if the path doesn't exist and the path has an extension it's a file, if it doesn't it's a directory. Something like this would suffice:

private boolean isPathDirectory(String myPath) {
    File test = new File(myPath);

    // check if the file/directory is already there
    if (!test.exists()) {
        // see if the file portion it doesn't have an extension
        return test.getName().lastIndexOf('.') == -1;
    } else {
        // see if the path that's already in place is a file or directory
        return test.isDirectory();
    }
}

Solution 2

So i want to test if the given string complies to a directory format or complies to a file format, in a multiplatform context (so, should work on Windows, Linux and Mac Os X).

In Windows, a directory can have an extension and a file is not required to have an extension. So, you can't tell just by looking at the string.

If you enforce a rule that a directory doesn't have an extension, and a file always has an extension, then you can determine the difference between a directory and a file by looking for an extension.

Solution 3

Why not just wrap them in a call to File#exists()?

File file = new File(...);
if (file.exists()) {

    // call isFile() or isDirectory()

}

By doing that, you've effectively negated the "exists" portion of isFile() and isDirectory(), since you're guaranteed that it does exist.


It's also possible that I've misunderstood what you're asking here. Given the second part of your question, are you trying to use isFile() and isDirectory() on non-existent files to see if they look like they're files or directories?

If so, that's going to be tough to do with the File API (and tough to do in general). If /foo/bar/baz doesn't exist, it's not possible to determine whether it's a file or a directory. It could be either.

Share:
20,770
aleroot
Author by

aleroot

I used to hate C++ until I suddenly fell in love with it :-)

Updated on July 25, 2021

Comments

  • aleroot
    aleroot almost 3 years

    I want to check if a given String is a file or a directory, i've tried the methods isFile() and isDirectory() of the File class but the problem is that if the directory or file doesn't exist these methods returns false, because as stated in the javadoc :

    isFile() :

    true if and only if the file denoted by this abstract pathname exists and is a normal file; false otherwise

    isDirectory() :

    true if and only if the file denoted by this abstract pathname exists and is a directory; false otherwise

    Basically i need two methods without the exist clause ...

    So i want to test if the given string complies to a directory format or complies to a file format, in a multiplatform context (so, should work on Windows, Linux and Mac Os X).

    Does exist some library that provide these methods ? What could be the best implementation of these methods ?

    UPDATE

    In the case of a string that could be both(without extension) by default should be identified as directory, if a file with that path does not exist.

  • Bhesh Gurung
    Bhesh Gurung over 12 years
    "It could be either." And it could be neither.
  • aleroot
    aleroot over 12 years
    In the case of a string that could be both(without extension) by default should be identified as directory, if a file with that path does not exist.
  • Rob Hruska
    Rob Hruska over 12 years
    @aleroot - That's something that your own business logic will have to enforce, since file extensions are mostly based on convention and don't really matter to most filesystems.
  • Brian Rogers
    Brian Rogers over 12 years
    "you're guaranteed that it does exist" ... unless some other process has deleted the file/directory in between the call to file.exists() and isFile(). Unlikely, perhaps, but possible.
  • bestsss
    bestsss over 12 years
    @BrianRogers, it can be the same process, just another thread.
  • Brian Rogers
    Brian Rogers over 12 years
    @bestsss - True; same idea though.