Below snippet return true, if the folderPath is a regular file and not a symbolic link.
Files.isDirectory(folderPath, LinkOption.NOFOLLOW_LINKS);
Let’s experiment it with below example.
$ls -lart
total 0
drwxrwxrwt 17 root wheel 544 Jan 8 17:53 ..
drwxr-xr-x 2 krishna wheel 64 Jan 8 20:05 demo
lrwxr-xr-x 1 krishna wheel 4 Jan 8 20:05 demo_sym_link -> demo
drwxr-xr-x 4 krishna wheel 128 Jan 8 20:05 .
As you see the above snippet,
a. Folder ‘demo’ is a regular directory
b. Folder demo_sym_link is a symbolic link pointing to the directory demo
Find the below working application.
RegularFolderCheck.java
package com.sample.app.files;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.util.Objects;
public class RegularFolderCheck {
public static boolean isFolder(final String folderPath) {
Objects.requireNonNull(folderPath);
return isFolder(new File(folderPath));
}
public static boolean isFolder(final File folder) {
Objects.requireNonNull(folder);
return isFolder(folder.toPath());
}
public static boolean isFolder(final Path folderPath) {
Objects.requireNonNull(folderPath);
return Files.isDirectory(folderPath, LinkOption.NOFOLLOW_LINKS);
}
public static void main(String[] args) {
final String folder1 = "/Users/Shared/filePrograms/demo";
final String folder2 = "/Users/Shared/filePrograms/demo_sym_link";
System.out.println("Is '" + folder1 + "' regular folder ? " + isFolder(folder1));
System.out.println("Is '" + folder2 + "' regular folder ? " + isFolder(folder2));
}
}
Output
Is '/Users/Shared/filePrograms/demo' regular folder ? true Is '/Users/Shared/filePrograms/demo_sym_link' regular folder ? false
You may like
file and stream programs in Java
Get POSIX file attributes in Java
Get file basic file attributes in Java
check whether a file is executable or not in Java
No comments:
Post a Comment