Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manipulating Windows file permissions in Java

Tags:

java

windows

Using Java how could I manipulate the access permissions of a file in Windows?

like image 499
anoop john Avatar asked Jul 29 '09 07:07

anoop john


People also ask

How do I change permissions on 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.

Which type of file access is permitted in Java?

A file in Java can have any combination of the following permissions: Executable. Readable. Writable.


2 Answers

If you are using Java 6, File class gives you setExecutable, setWritable, etc. See: http://java.sun.com/javase/6/docs/api/java/io/File.html

On older Java versions this is not possible; you have to exec OS commands to do that:

Windows:

Runtime.getRuntime().exec("attrib -r myFile");

Unix:

Runtime.getRuntime().exec("chmod 777 myFile");
like image 50
Zed Avatar answered Oct 12 '22 22:10

Zed


The new Java 7 java.nio.file.attribute package makes all of this a lot easier. It provides views onto the complete set of file attributes, including Posix file permissions.

like image 23
Brian Agnew Avatar answered Oct 12 '22 22:10

Brian Agnew