173

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?

GGets
  • 416
  • 6
  • 19
Egor
  • 39,695
  • 10
  • 113
  • 130
  • 3
    "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. – user207421 Oct 13 '16 at 10:19

8 Answers8

253

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
Baz
  • 36,440
  • 11
  • 68
  • 94
  • As I mentioned in my question, I have only Strings and no File instances, and I can't create them. – Egor Oct 08 '12 at 11:08
  • 1
    `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. – Baz Oct 08 '12 at 11:09
  • 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? – Egor Oct 08 '12 at 11:19
  • @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. – Baz Oct 08 '12 at 11:22
  • 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. – Egor Oct 08 '12 at 11:24
  • @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? – Baz Oct 08 '12 at 11:29
  • 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. – Egor Oct 08 '12 at 11:37
  • @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? – Baz Oct 08 '12 at 11:39
  • 14
    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.. – Praful Bhatnagar Oct 09 '12 at 08:57
  • 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(). – instanceof Mar 06 '17 at 14:19
  • @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)`. – AndrewF Apr 12 '19 at 23:55
64

Clean solution while staying with the nio API:

Files.isDirectory(path)
Files.isRegularFile(path)
pgsandstrom
  • 14,361
  • 13
  • 70
  • 104
  • 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 – Kervvv Jul 06 '18 at 02:02
  • 7
    Doesn't answer the question asked. *Files.isDirectory( )* does not accept a String. – gerardw Feb 06 '19 at 14:16
  • 2
    Starting from `String`? No problem... `Path path = Paths.get(myString);` and you're ready to go ! – Stephan Mar 20 '22 at 16:48
21

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);
}
Sheng
  • 1,697
  • 4
  • 19
  • 33
  • 3
    Why give an answer in Scala when the question is asking for Java code (see tags)? – Baz Oct 22 '15 at 15:58
  • 6
    @Baz Because Scala is covariant to Java... just kidding :-D . I have updated the answer. – Sheng Oct 23 '15 at 14:29
  • 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. – Gondri Oct 11 '18 at 10:02
9

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

Emdadul Sawon
  • 5,730
  • 3
  • 45
  • 48
4
String path = "Your_Path";
File f = new File(path);

if (f.isDirectory()){



  }else if(f.isFile()){



  }
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
2

To check if a string represents a path or a file programatically, you should use API methods such as isFile(), isDirectory().

How does system understand whether there's a file or a folder?

I guess, the file and folder entries are kept in a data structure and it's managed by the file system.

Juvanis
  • 25,802
  • 5
  • 69
  • 87
1
public static boolean isDirectory(String path) {
    return path !=null && new File(path).isDirectory();
}

To answer the question directly.

gerardw
  • 5,822
  • 46
  • 39
0
   private static boolean isValidFolderPath(String path) {
    File file = new File(path);
    if (!file.exists()) {
      return file.mkdirs();
    }
    return true;
  }
amod
  • 4,190
  • 10
  • 49
  • 75