Check if a path represents a file or a folder

JavaAndroidFilePathDirectory

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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 - Java

Clean solution while staying with the nio API:

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

Solution 3 - Java

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 - Java

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 - Java

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

if (f.isDirectory()){



  }else if(f.isFile()){



  }

Solution 6 - Java

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.

Solution 7 - Java

public static boolean isDirectory(String path) {
    return path !=null && new File(path).isDirectory();
}

To answer the question directly.

Solution 8 - Java

   private static boolean isValidFolderPath(String path) {
    File file = new File(path);
    if (!file.exists()) {
      return file.mkdirs();
    }
    return true;
  }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEgorView Question on Stackoverflow
Solution 1 - JavaBazView Answer on Stackoverflow
Solution 2 - JavapgsandstromView Answer on Stackoverflow
Solution 3 - JavaShengView Answer on Stackoverflow
Solution 4 - JavaEmdadul SawonView Answer on Stackoverflow
Solution 5 - JavaKumar Vivek MitraView Answer on Stackoverflow
Solution 6 - JavaJuvanisView Answer on Stackoverflow
Solution 7 - JavagerardwView Answer on Stackoverflow
Solution 8 - JavaKaweesi JosephView Answer on Stackoverflow