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?
-
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.– user207421Oct 13, 2016 at 10:19
8 Answers
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
-
As I mentioned in my question, I have only Strings and no File instances, and I can't create them.– EgorOct 8, 2012 at 11:08
-
1
path
in my example is theString
. Why can't you create aFile
instance? Note that this will not change anything on the filesystem.– BazOct 8, 2012 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?– EgorOct 8, 2012 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.– BazOct 8, 2012 at 11:22 -
12isDirectory() 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.. Oct 9, 2012 at 8:57
Clean solution while staying with the nio API:
Files.isDirectory(path)
Files.isRegularFile(path)
-
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– KervvvJul 6, 2018 at 2:02 -
7Doesn't answer the question asked. Files.isDirectory( ) does not accept a String.– gerardwFeb 6, 2019 at 14:16
-
Starting from
String
? No problem...Path path = Paths.get(myString);
and you're ready to go !– StephanMar 20 at 16:48
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);
}
-
3Why give an answer in Scala when the question is asking for Java code (see tags)?– BazOct 22, 2015 at 15:58
-
6@Baz Because Scala is covariant to Java... just kidding :-D . I have updated the answer.– ShengOct 23, 2015 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.– GondriOct 11, 2018 at 10:02
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
String path = "Your_Path";
File f = new File(path);
if (f.isDirectory()){
}else if(f.isFile()){
}
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.
public static boolean isDirectory(String path) {
return path !=null && new File(path).isDirectory();
}
To answer the question directly.
private static boolean isValidFolderPath(String path) {
File file = new File(path);
if (!file.exists()) {
return file.mkdirs();
}
return true;
}