Roy Rico Roy Rico - 1 month ago 27
Java Question

How do i programmatically change file permissions?

In Java, I'm dynamically creating a set of files and I'd like to change the file permissions on these files on a linux/unix file system. I'd like to be able to execute the Java equivalent of

chmod
. Is that possible Java 5? If so, how?

I know in Java 6 the File object has setReadable()/setWritable() methods. I also know I could make a system call to do this, but I'd like to avoid that if possible.

Answer

Full control over file attributes is available in Java 7, as part of the "new" New IO facility (NIO.2). For example, POSIX permissions can be set with setPosixFilePermissions().

In earlier versions of Java, using native code of your own, or exec-ing command-line utilities are common approaches.