'Files.isExecutable' method returns true if the file exists and is executable and return false in below cases.
a. if the file does not exist or file exists and not executable
b. Execute access is denied because the Java virtual machine has insufficient privileges, or
c. Access cannot be determined.
Example
boolean isExecutable = Files.isExecutable(Paths.get(filePath));
Find the below working application.
FileExecutableCheck.java
package com.sample.app.files;
import java.nio.file.Files;
import java.nio.file.Paths;
public class FileExecutableCheck {
public static void main(String[] args) {
final String filePath = "/Users/Shared/filePrograms/a.txt";
boolean isExecutable = Files.isExecutable(Paths.get(filePath));
System.out.println("Is " + Paths.get(filePath).getFileName() + " executable ? " + isExecutable);
}
}
Output
Is a.txt executable ? false
You may like
file and stream programs in Java
Read the data from BufferedReader in Java
Read file content using BufferedReader in Java
Touch the file in Java (Update the file timestamp)
No comments:
Post a Comment