9

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.

aleroot
  • 71,077
  • 30
  • 176
  • 213
  • Apache commons.io could help you. Maybe using FileUtils.sizeof() or something like that... – Alfabravo Dec 27 '11 at 20:11
  • 3
    How will Java know if a pathname pointing to something that doesn't exist is a file or a directory if there's nothing there? There's no pathname format to determine if something is a file or directory across all OSes. – Jon Lin Dec 27 '11 at 20:13
  • What you want is not possible, there is no difference between a file and a directory unless it actually exists in the file system (and has the necessary attributes the OS provides) Extensions and the like might be just your own assumption. Side note: extension doesn't exist either nowadays, it is an old, old artifact coming from DOS. – bestsss Dec 28 '11 at 00:22

4 Answers4

8

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.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
4

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.

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
  • 1
    "It could be either." And it could be neither. – Bhesh Gurung Dec 27 '11 at 20:17
  • 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. – aleroot Dec 27 '11 at 20:30
  • @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. – Rob Hruska Dec 27 '11 at 20:31
  • "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. – Brian Rogers Dec 27 '11 at 20:33
  • @BrianRogers, it can be the same process, just another thread. – bestsss Dec 28 '11 at 00:23
  • @bestsss - True; same idea though. – Brian Rogers Dec 28 '11 at 00:34
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();
    }
}
Eric Rahm
  • 688
  • 3
  • 5
0

There are rules for what is invalid in a file and/or folder name. For example, Windows doesn't allow *, ?, and a few other characters. Based on what's invalid, you could build a regex expression or some other process/checking system to see if it looks like a file or folder.

This could get complex as you want it to work for many different OS's. Also, as previously posted, there would be no way to tell a file from a folder unless you artificially enforced a convention. For example, directories must end in a front-slash / in Windows.

Having the IF EXISTS check first would help. If IF EXISTS = true, then running the existing File.isDirectory() or File.isFile() code would simplify a lot of this. You would only have to write code for when IF EXISTS = false.

Sam
  • 8,330
  • 2
  • 26
  • 51