Is there any way to read/set Unix rights for a file and check if process has user/group/other rights in Java? I know it's not a problem with JNI, but I need cross-platform solution without JNI.
setWritable() − This method is used to set the write permissions to the file represented by the current (File) object. setReadable() − This method is used to set the read permissions to the file represented by the current (File) object.
Program. The java. io. FileOutputStream implies(Permission p) method tests if this FilePermission object "implies" the specified permission.
Permissions of 644 mean that the owner of the file has read and write access, while the group members and other users on the system only have read access.
-rw-r--r-- (644) -- Only user has read and write permissions; the group and others can read only. -rwx------ (700) -- Only the user has read, write and execute permissions. -rwxr-xr-x (755) -- The user has read, write and execute permissions; the group and others can only read and execute.
Set<PosixFilePermission> filePerm = null;
try {
filePerm = Files.getPosixFilePermissions(Paths.get("/app/data/abc.txt"));
} catch (IOException e) {
e.printStackTrace();
}
String permission = PosixFilePermissions.toString(filePerm);
Supported from Java 1.7, not cross-platform.
This is going to be possible with Java 7, which is going to be released this summer, using the classes in the package java.nio.file.attribute
. See the JDK 7 API documentation.
Java 6 and older do not have anything in the standard library to work with Unix file system rights.
So, by using the Java 7 API you won't need JNI but as Voo says it is ofcourse not going to be cross-platform, because Windows and other non-Unix systems do not support Unix file rights.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With