Home » Core Java » io » File » Set File permissions in Java example

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

Set File permissions in Java example

With this example we shall show you how to set file permissions with Java. This is a very important task to take into consideration when you’re working in a shared system. Although this operation is operation system specific, Java offers a generic API that you can use in all platforms.

As you know in UNIX systems you can go much further than just setting a file to be readable, writable or executable. You can set the permissions you want for all users and groups that share the system and not only for the owner of the file. For example you can do chmod 744 filename. Java does not offer that kind of flexibility in UNIX environments, but you can always use : Runtime.getRuntime().exec("chmod 744 filename").

In short, all you have to do in order to find out or set file permissions in Java is:

  • Use File.canExecute(), File.canWrite() and File.canRead() methods to see the respective permissions of a specific file
  • Use File.setReadable(boolean), File.setWritable(boolean) and File.setExecutable(boolean) methods to set the respective permissions of a specific file

Let’s see the code:

package com.javacodegeeks.java.core;

import java.io.File;
import java.io.IOException;

public class JavaFilePermissionsExample {
	public static void main(String[] args) {
		try {

			File scriptfile = new File("C:\\Users\\nikos7\\Desktop\\words");

			if (scriptfile.exists()) {
				System.out.println("Is file executable: "+ scriptfile.canExecute());
				System.out.println("Is file writable : "+ scriptfile.canWrite());
				System.out.println("Is file readable : " + scriptfile.canRead());
			}

			scriptfile.setReadable(true);
			scriptfile.setWritable(true);
			scriptfile.setExecutable(true);

			System.out.println("\nIs file executable : "+ scriptfile.canExecute());
			System.out.println("Is file writable : " + scriptfile.canWrite());
			System.out.println("Is file readable :  " + scriptfile.canRead());

			if (scriptfile.createNewFile()) {
				System.out.println("\nFile has been succesfully created");
			} else {
				System.out.println("\nFile already exists!");
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Output:

Is file executable: true
Is file writable : false
Is file readable : true

Is file executable : true
Is file writable : true
Is file readable :  true

File already exists!

 
This was an example of how to set and see the read, write and execute permissions of file in Java.

(No Ratings Yet)
1 Comment Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Fholisani Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Fholisani
Guest
Fholisani

Will the operation fail if the user/Application does not have permission to change the access permissions of this abstract path name