Check if a path represents a file or a folder

175,334

Solution 1

Assuming path is your String.

File file = new File(path);

boolean exists =      file.exists();      // Check if the file exists
boolean isDirectory = file.isDirectory(); // Check if it's a directory
boolean isFile =      file.isFile();      // Check if it's a regular file

See File Javadoc


Or you can use the NIO class Files and check things like this:

Path file = new File(path).toPath();

boolean exists =      Files.exists(file);        // Check if the file exists
boolean isDirectory = Files.isDirectory(file);   // Check if it's a directory
boolean isFile =      Files.isRegularFile(file); // Check if it's a regular file

Solution 2

Clean solution while staying with the nio API:

Files.isDirectory(path)
Files.isRegularFile(path)

Solution 3

Please stick to the nio API to perform these checks

import java.nio.file.*;

static Boolean isDir(Path path) {
  if (path == null || !Files.exists(path)) return false;
  else return Files.isDirectory(path);
}

Solution 4

There is no way for the system to tell you if a String represent a file or directory, if it does not exist in the file system. For example:

Path path = Paths.get("/some/path/to/dir");
System.out.println(Files.isDirectory(path)); // return false
System.out.println(Files.isRegularFile(path)); // return false

And for the following example:

Path path = Paths.get("/some/path/to/dir/file.txt");
System.out.println(Files.isDirectory(path));  //return false
System.out.println(Files.isRegularFile(path));  // return false

So we see that in both case system return false. This is true for both java.io.File and java.nio.file.Path

Solution 5

String path = "Your_Path";
File f = new File(path);

if (f.isDirectory()){



  }else if(f.isFile()){



  }
Share:
175,334

Related videos on Youtube

Egor
Author by

Egor

Software Developer at Square

Updated on February 03, 2021

Comments

  • Egor
    Egor over 3 years

    I need a valid method to check if a String represents a path for file or a directory. What are valid directory names in Android? As it comes out, folder names can contain '.' chars, so how does system understand whether there's a file or a folder?

    • user207421
      user207421 over 7 years
      "How does system understand whether there's a file or a folder": How can the system not understand? It's there on the disk in the file system and it is one or the other.
  • Egor
    Egor over 11 years
    As I mentioned in my question, I have only Strings and no File instances, and I can't create them.
  • Baz
    Baz over 11 years
    path in my example is the String. Why can't you create a File instance? Note that this will not change anything on the filesystem.
  • Egor
    Egor over 11 years
    Here's a concrete example, I'm trying to create a File using the following path: /mnt/sdcard/arc/root, and for isDirectory() it returns false. What's the issue here?
  • Baz
    Baz over 11 years
    @Egor Quite hard to tell, since I don't have an Android device. Note that root may be a file. Files don't necessarily have a .something extension.
  • Egor
    Egor over 11 years
    So I presume your solution doesn't work. root is a directory, but taking only its name and creating File instance doesn't tell me whether it's file or directory.
  • Baz
    Baz over 11 years
    @Egor So you tried: new File("/mnt/sdcard/arc/root").isDirectory() and it returned false? Are you sure it's a directory and there is no file with the same path?
  • Egor
    Egor over 11 years
    It's actually just a plain path and my actual problem is to know how I can check whether it relates to a file or to a folder.
  • Baz
    Baz over 11 years
    @Egor "It's actually just a plain path" What do you mean by that? Could you please answer the questions I asked in my previous comment?
  • Praful Bhatnagar
    Praful Bhatnagar over 11 years
    isDirectory() method would return true only if the file exists and it is an directory. If the file given in the path does not exists then also it return false. So it isDirectory() would return false if the path given does not exists or it exists but it is not a directory... Hope that helps..
  • Baz
    Baz over 8 years
    Why give an answer in Scala when the question is asking for Java code (see tags)?
  • Sheng
    Sheng over 8 years
    @Baz Because Scala is covariant to Java... just kidding :-D . I have updated the answer.
  • instanceof
    instanceof about 7 years
    Thank you, I needed to check if my string was a file, but according to the docs, the isFile() method only checks if a string is a "regular file"", so I ended up using !isDirectory().
  • Kervvv
    Kervvv almost 6 years
    This is the better answer if you are looping over a list of directories. Here you're using a static class to run these checks, rather than creating a new File object each time. Saves memory
  • Gondri
    Gondri over 5 years
    You can create temp directory, create there directories and files. Then use code above and assert. In one hand use regular files/directories otherwise use some dummy path of item which is not created.
  • gerardw
    gerardw over 5 years
    Doesn't answer the question asked. Files.isDirectory( ) does not accept a String.
  • AndrewF
    AndrewF about 5 years
    @instanceof, you would need to also check file.exists(), as otherwise, !file.isDirectory() will be true if nothing exists at the path. What you are calling a "file" is called a "regular file" in the Java API. This is because directories are also files but are not "regular files". As many have answered, the correct solution is to use file.isFile() or Files.isRegularFile(path).
  • Stephan
    Stephan about 2 years
    Starting from String? No problem... Path path = Paths.get(myString); and you're ready to go !