Quantcast
Channel: Guru – Jafaloo – The Tech Blog
Viewing all articles
Browse latest Browse all 30

How to Set the File Permission in Java?

$
0
0

Java comes with some handy tools to manipulate file permissions. Though Java File class has the ability to set the file permissions but it is not so versatile. The biggest drawback of Java’s file permission is that you can’t set the different file permissions for group and other users. That is you can divide the file permissions into two sets of users, owner and all other users.

For example you can not set different permissions for a file for different users and groups.

But Java 7 introduced PosixFilePermission (part of java.nio.file package) which allows us to set different permissions for different users and owners and groups. In this tutorial we shall see both type of examples before Java 7 and Java 7 file permissions.

Java File Permission Example

package com.lessonslab.io.file;

import java.io.File;

public class SetFilePermissionInJava
{
private static String MY_FILE = "/home/hduser/test/test.sh";

public static void main(String args[])
{
File file = new File(MY_FILE);
//Check if the files Exists
if(file.exists())
{
//Show the Present Permissions on the file
System.out.println("Is Readable: " + file.canRead());
System.out.println("Is Executable: " + file.canExecute());
System.out.println("Is Writable: " + file.canRead());
}
//Change the file permissions
file.setExecutable(true);
file.setReadable(false);
file.setWritable(false);
System.out.println("-----------New Permissions---------------");
System.out.println("Is Readable: " + file.canRead());
System.out.println("Is Executable: " + file.canExecute());
System.out.println("Is Writable: " + file.canRead());
}
}

Output

Is Readable: true
Is Executable: false
Is Writable: true
-----------New Permissions---------------
Is Readable: false
Is Executable: true
Is Writable: false

The above example is not capable of setting different file permissions to different users or groups.

Now lets see Java 7 example of setting file permission which can set different permissions for users and groups.

Java 7 File Permission Example

package com.lessonslab.io.file;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission;
import java.util.HashSet;
import java.util.Set;

public class SetFilePermissionInJava7
{
private static String MY_FILE = "/home/hduser/test/test.sh";

public static void main(String args[])
{
//Lets use PosixFilePermission to set file permissions
Set<PosixFilePermission> filePerms = new HashSet<PosixFilePermission>();

//Owner File permission
filePerms.add(PosixFilePermission.OWNER_READ);
filePerms.add(PosixFilePermission.OWNER_WRITE);
filePerms.add(PosixFilePermission.OWNER_EXECUTE);

//For Group File Permissions (Only Read and Write for groups)
filePerms.add(PosixFilePermission.GROUP_READ);
filePerms.add(PosixFilePermission.GROUP_WRITE);

//For Others File permissions (Only read permission for others)
filePerms.add(PosixFilePermission.OTHERS_READ);
System.out.println("Done!!!");

try
{
Files.setPosixFilePermissions(Paths.get(MY_FILE), filePerms);
}
catch (IOException e)
{
System.out.println("Unable to change file permissions :" + e.getMessage());
}
}

}

You can refer above example codes to manipulate file permissions in your application as well. Hope this tutorial was helpful for you.

The post How to Set the File Permission in Java? appeared first on Jafaloo - The Tech Blog.


Viewing all articles
Browse latest Browse all 30

Trending Articles