Java IO Tutorial

How to set the file permission in Java

In Java, file permissions are very OS specific: *nix , NTFS (windows) and FAT/FAT32, all have different kind of file permissions. Java comes with some generic file permission to deal with it.

Check if the file permission allow :

  1. file.canExecute(); – return true, file is executable; false is not.
  2. file.canWrite(); – return true, file is writable; false is not.
  3. file.canRead(); – return true, file is readable; false is not.

Set the file permission :

  1. file.setExecutable(boolean); – true, allow execute operations; false to disallow it.
  2. file.setReadable(boolean); – true, allow read operations; false to disallow it.
  3. file.setWritable(boolean); – true, allow write operations; false to disallow it.

In *nix system, you may need to configure more specifies about file permission, e.g set a 777 permission for a file or directory, however, Java IO classes do not have ready method for it, but you can use the following dirty workaround :


Runtime.getRuntime().exec("chmod 777 file");

File permission example


package com.mkyong.file;

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

public class FilePermissionExample 
{
    public static void main( String[] args )
    {	
    	try {
    		 
	      File file = new File("/mkyong/shellscript.sh");
	      
	      if(file.exists()){
	    	  System.out.println("Is Execute allow : " + file.canExecute());
		  System.out.println("Is Write allow : " + file.canWrite());
		  System.out.println("Is Read allow : " + file.canRead());
	      }
	         
	      file.setExecutable(false);
	      file.setReadable(false);
	      file.setWritable(false);
	      
	      System.out.println("Is Execute allow : " + file.canExecute());
	      System.out.println("Is Write allow : " + file.canWrite());
	      System.out.println("Is Read allow : " + file.canRead());
	      
	      if (file.createNewFile()){
	        System.out.println("File is created!");
	      }else{
	        System.out.println("File already exists.");
	      }
	      
    	} catch (IOException e) {
	      e.printStackTrace();
	    }
    }
}

Reference

  1. http://java.sun.com/javase/6/docs/api/java/io/File.html

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
9 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
amol
7 years ago

hi sir r
i need code in java to restrict copy,move,etc,,…….only read permission …just like locker
pls suggest

SOLDIER
8 years ago

good site

edo
10 years ago

Nice posting mr mkyong

outlet
10 years ago

I am just writing to make you know what a great discovery my wife’s girl gained using your site. She came to understand a lot of issues, with the inclusion of what it’s like to possess an amazing helping heart to have most people very easily fully understand certain very confusing things. You really surpassed our own desires. Thanks for giving those informative, safe, explanatory as well as unique tips about that topic to Tanya.

I.Sahin
11 years ago

Hi,

I want to set permissions as block to delete file but this methods are not enough for this.

Thanks.

Pankaj
11 years ago

Java 7 has made it very easy to set file permissions, File class is not versatile and doesn’t have option for group and others, read this post to know how we can do it very easily now.

http://www.journaldev.com/855/how-to-set-file-permissions-in-java-easily-using-java-7-posixfilepermission

silver
12 years ago

So, when i run this program I got the output as below:

exec- false
read – true
write – true } – which I understand.

but after setting all permissions to ‘false’ also, I got the same output.

exec- false
read – true
write – true } – which I don’t understand.

I am running the file on Windows OS. Can anyone please explain where I got wrong.

Thanks much in advance!!

wangyanfeng
10 years ago
Reply to  silver

Returns:
true if and only if the operation succeeded. The operation will fail if the user does not have permission to change the access permissions of this abstract pathname. If readable is false and the underlying file system does not implement a read permission, then the operation will fail.

Sreedhar
13 years ago

Hi all

How to copy files from server to client with out asking open/save.i just want transfer a file server to client. Please help me.

Thanks In advance