Created
September 25, 2017 13:25
-
-
Save javamultiplex/b906b8a426e0b3ea612c1c8ede8f7c81 to your computer and use it in GitHub Desktop.
How to get file permissions in Java?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javamultiplex.filehandling; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.Scanner; | |
/** | |
* | |
* @author Rohit Agarwal | |
* @category File Handling | |
* @problem How to get given file permissions in Java? | |
* | |
*/ | |
public class FilePermission { | |
public static void main(String[] args) throws IOException { | |
Scanner input = null; | |
try { | |
input = new Scanner(System.in); | |
System.out.println("Enter file name with extension : "); | |
String fileName = input.nextLine(); | |
if (isValidFileName(fileName)) { | |
// file present in current working directory. | |
File file = new File(fileName); | |
if (file.exists()) { | |
System.out.println("Executable : " + file.canExecute()); | |
System.out.println("Readable : " + file.canRead()); | |
System.out.println("Writable : " + file.canWrite()); | |
} else { | |
System.out.println("File is not present in current directory."); | |
} | |
} else { | |
System.out.println("File name is not valid."); | |
} | |
} finally { | |
if (input != null) { | |
input.close(); | |
} | |
} | |
} | |
private static boolean isValidFileName(String fileName) { | |
// Regular expression for validating file name. | |
String pattern = "^.+\\..+$"; | |
boolean result = false; | |
if (fileName.matches(pattern)) { | |
result = true; | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment