java.io.File
provide several methods to get or set file permissions in java code. But the drawback of java.io.File
‘s methods is that it can only separate file permission into two groups which is owner or all users.
So java 7 introduce java.nio.file.attribute.PosixFilePermission
which provide methods that can let you define file permission more accurately for different group of users in *nix os. This article will show examples about how to do that.
1. Get / Set File Permission Using java.io.File.
Below is code snippets to get read, write and execute permission. Return true means can read, can write or can execute.
boolean fileCanRead = fileObject.canRead(); boolean fileCanWrite = fileObject.canWrite(); boolean fileCanExecute = fileObject.canExecute();
Below is code snippets to set read, write and execute permission.
fileObject.setReadable(true); fileObject.setWritable(false); fileObject.setExecutable(false);
Full Example Code
public void setFilePermissionWithJavaFile() { try { // First get current working directory. String currentDir = System.getProperty("user.dir"); // Get current os seperator. String fileSep = File.separator; // Build the test filepath. String newFilePath = currentDir + fileSep + "testFilePermission.txt"; System.out.println("New filepath : " + newFilePath); // Create new fileobject. File fileObject = new File(newFilePath); // If not exist then create it. if(!fileObject.exists()) { fileObject.createNewFile(); } // Get read, write and execute permissions. boolean fileCanRead = fileObject.canRead(); boolean fileCanWrite = fileObject.canWrite(); boolean fileCanExecute = fileObject.canExecute(); // Print out current permission. System.out.println("This file's permission is listed below : "); System.out.println("Can read ? : " + fileCanRead); System.out.println("Can write ? : " + fileCanWrite); System.out.println("Can execute ? : " + fileCanExecute); // Change permission as below. fileObject.setReadable(true); fileObject.setWritable(false); fileObject.setExecutable(false); // Get this file's read, write and execute permissions again after change. fileCanRead = fileObject.canRead(); fileCanWrite = fileObject.canWrite(); fileCanExecute = fileObject.canExecute(); // Print out the file's permissions after change. System.out.println("This file's permission is listed below : "); System.out.println("Can read ? : " + fileCanRead); System.out.println("Can write ? : " + fileCanWrite); System.out.println("Can execute ? : " + fileCanExecute); }catch(Exception ex) { ex.printStackTrace(); } }
2. Set Permission Using Java 7 PosixFilePermission.
You should import java.nio.file.Files
; java.nio.file.Paths
; java.nio.file.attribute.PosixFilePermission
classes in this example. They are all java new io classes.
If you are not familiar with Linux file permission, you can read Linux User Group And File Permission Introduction first.
public void setFilePermissionInLinux() { try { File fileObject = new File("/Users/dev2qa/jerry/test.txt"); // If file not exist then create it. if(!fileObject.exists()) { fileObject.createNewFile(); } // Set read, write and execute permission to owner only. fileObject.setReadable(true, true); fileObject.setWritable(true, true); fileObject.setExecutable(true, true); // Set read, write and execute permission to all users. // After settings, permission will change to 777. fileObject.setReadable(true, false); fileObject.setWritable(true, false); fileObject.setExecutable(true, false); // If you want to set permission more accurately // for owner, owner group users and other group users in Linux. // You can use below java code. Set<PosixFilePermission> pfpSet = new HashSet(); // Add read, write and execute permission to owner. pfpSet.add(PosixFilePermission.OWNER_READ); pfpSet.add(PosixFilePermission.OWNER_WRITE); pfpSet.add(PosixFilePermission.OWNER_EXECUTE); // Add read, write permission to owner group users. pfpSet.add(PosixFilePermission.GROUP_READ); pfpSet.add(PosixFilePermission.GROUP_WRITE); // Add execute permission to other group users pfpSet.add(PosixFilePermission.OTHERS_EXECUTE); // Assign permissions. Files.setPosixFilePermissions(Paths.get("/Users/dev2qa/jerry/test.txt"), pfpSet); // After the code execute test.txt will have a permission of 761 }catch(Exception ex) { ex.printStackTrace(); } }
[download id=”3102″]