Saturday 7 January 2023

check whether a file is executable or not in Java

'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)

Get POSIX file attributes in Java

Get file basic file attributes in Java

No comments:

Post a Comment