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.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist