Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Unix file rights in Java

Tags:

java

unix


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.

like image 660
Ivan Mushketyk Avatar asked Jun 22 '11 20:06

Ivan Mushketyk


People also ask

How can I give permission to a file in Java?

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.

How do I check permissions in Java?

Program. The java. io. FileOutputStream implies(Permission p) method tests if this FilePermission object "implies" the specified permission.

What is 644 permission Unix?

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.

How do I give permission to RW R?

-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.


2 Answers

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.

like image 184
sharathchandra.ck Avatar answered Sep 26 '22 22:09

sharathchandra.ck


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.

like image 34
Jesper Avatar answered Sep 25 '22 22:09

Jesper