security

Check read/write permission for a directory example

This is an example of how to check read/write permission for a directory. Checking read/write permission for a directory implies that you should:

  • Create a FilePermission for the pathname of the directory, and a comma-separated list of the actions granted on the directory.
  • Invoke the checkPermission(Permission perm) API method of the AccessController, in order to check whether the access request indicated by the permission created above should be allowed or denied. If the permission is denied a SecurityException is thrown.

Let’s take a look at the code snippet that follows:
 

package com.javacodegeeks.snippets.core;

import java.io.FilePermission;
import java.security.AccessController;

public class CheckDirPermissions {

  public static void main(String[] args) {

String path = "/home/*";

String actions = "read,write";

try {
	  AccessController.checkPermission(new FilePermission(path, actions));

	  System.out.println("You have read/write permition to use : " + path);

} catch (SecurityException e) {
	  System.out.println("You have read/write permition to use : " + path);

}

  }
}

Output:

You have read/write permition to use : /home/*

 
This was an example of how to check read/write permission for a directory in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button