devxlogo

File permissions

File permissions

Question:
How do you change UNIX file permissions in Java? I’ve seen it done and I need to replicate it.

Answer:
There is no platform-independent way of changingfile permissions in Java. To do so, you eitherhave to write native code, or exec a localprogram to do the work for you.

The way the programs you’ve seen have done itis probably using the latter approach. Forexample:

Runtime runtime = Runtime.getRuntime();Process chmod;chmod = runtime.exec("/bin/chmod 755 filename");chmod.waitFor();

This approach is not recommended for programsthat must portable. But it is an acceptablemethod if your program is only going to run onUnix systems and you just need to get thingsworking.

devx-admin

Share the Post: