Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Checking File Permissions 
2.1.
canExecutable
2.1.1.
Example
2.2.
canRead
2.2.1.
Example
2.3.
canWrite
2.3.1.
Example
3.
Changing File Permissions 
3.1.
setExecutable
3.1.1.
Example
3.2.
setRead
3.2.1.
Example
3.3.
setWrite
3.3.1.
Example
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

File Permissions

Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

When working with files it is very important to know what permissions do we have with the particular files. Thus, to make our lives slightly easier, java has numerous methods to call and check the permissions on the file we are working on. Permissions can be checked on three levels: execution-only, read-only, write. Execution-only means that the application is allowed to execute the file. Read-only denotes that the particular file can only be read by the application. Write means that the application can write in the file and change its contents.

Along with determining the various file permissions we can also change them to suit our needs for our application. In this blog, we will learn about the various methods that can be used to manage permissions using Java.

Also Read, Multithreading in java, and Duck Number in Java.

Checking File Permissions 

A major part of manipulating any file is first knowing the permissible changes that can be made to the file we are working on. This is made significantly easier by the methods introduced in Java.

canExecutable

canExecutable can be used to check whether the file we are searching for actually exists by checking the abstract file pathname. Along with this, it also checks whether the application is allowed to execute the file or not. 

Example

import java.io.*;

public class test{
	public static void main(String[] args){
		File file = new File("D:\\test.txt");
		System.out.println(file.canExecute()); //Prints true if pathname exists and application is allowed to execute it.
    }
}

canRead

canRead checks whether the application is allowed to read data from the file denoted by the pathname.

Example

import java.io.*;

public class test{
	public static void main(String[] args){
		File file = new File("D:\\test.txt");
		System.out.println(file.canRead()); //Prints true if the application is allowed to read data from it.
    }
}

canWrite

canWrite basically checks whether we can write into the file specified by the abstract pathname. Write means that we are allowed to make changes into the content of the files.

Example

import java.io.*;

public class test{
	public static void main(String[] args){
		File file = new File("D:\\test.txt");
		System.out.println(file.canWrite()); //Prints true if pathname exists and application is allowed to write to the file.
    }
}
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Changing File Permissions 

Now that we know how to check the file permissions of any pathname provided, we just need to know how to manipulate these permissions so that we can suit the file according to our needs and make sure that the files are accessible at the particular levels of permissions at the request of the programmer.

setExecutable

setExecutable can change the permission to execute the file accessed by the pathname.

Example

import java.io.*;

public class test{
	public static void main(String[] args){
		File file = new File("D:\\test.txt");
        file.setExecutable(true);
		System.out.println(file.canExecute()); //Prints true as the permission was changed
    }
}

setRead

setRead changes the read permission of a file to the value given as a parameter. However, there is a chance of failure if the user doesn’t have the permission to the specified path name.

Example

import java.io.*;

public class test{
	public static void main(String[] args){
		File file = new File("D:\\test.txt");
        file.setRead(true);
		System.out.println(file.canRead()); //Prints true as the permission was changed
    }
}

setWrite

Similar to other set operations on files, this changes the permission of the file in the pathname, however, has the same vulnerability of failing if the user doesn’t have permission to the specified pathname.

Example

import java.io.*;

public class test{
	public static void main(String[] args){
		File file = new File("D:\\test.txt");
        file.setExecutable(true);
		System.out.println(file.canExecute()); //Prints true as the permission was changed
    }
}


Practice it on online java compiler for better understanding.

FAQs

1. What is the specific class that contains these methods?

Ans: The java.io.FilePermission class contains the methods specified in this blog.

 

2. What are some other methods in FilePermission?

Ans: Some other methods regarding delete and readlink permissions are File.delete and readSymbolicLink.

 

3. What are the types of methods in Java?

Ans: There are two types of methods, user-defined methods,  and standard library methods.

 

4. How do we convert a file into a path?

Ans: We can convert the file into a path by using the toPath() method.

 

Key Takeaways

In this blog, we discussed how we can get the permissions on a particular file and how to manipulate it to suit the programmer.

You may want to learn more about different types of methods in java here.

Learning never stops, and to feed your quest to learn and become more skilled, head over to our practice platform Coding Ninjas Studio to practise top problems, attempt mock tests, read interview experiences, and much more.!

Happy Learning!

Previous article
Delete a file using Java
Next article
File Reader
Live masterclass