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()){
}
Related videos on Youtube
Comments
-
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 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 over 11 yearsAs I mentioned in my question, I have only Strings and no File instances, and I can't create them.
-
Baz over 11 years
path
in my example is theString
. Why can't you create aFile
instance? Note that this will not change anything on the filesystem. -
Egor over 11 yearsHere'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 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 over 11 yearsSo 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 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 over 11 yearsIt'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 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 over 11 yearsisDirectory() 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 over 8 yearsWhy give an answer in Scala when the question is asking for Java code (see tags)?
-
Sheng over 8 years@Baz Because Scala is covariant to Java... just kidding :-D . I have updated the answer.
-
instanceof about 7 yearsThank 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 almost 6 yearsThis 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 over 5 yearsYou 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 over 5 yearsDoesn't answer the question asked. Files.isDirectory( ) does not accept a String.
-
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 usefile.isFile()
orFiles.isRegularFile(path)
. -
Stephan about 2 yearsStarting from
String
? No problem...Path path = Paths.get(myString);
and you're ready to go !