Using Java how could I manipulate the access permissions of a file in Windows?
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.
A file in Java can have any combination of the following permissions: Executable. Readable. Writable.
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");
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.
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