How to change permissions(Read, Write and Execute) of given file in Java?
Problem Description :
Write a program in Java that checks permissions of given file and accordingly set new permission.
How to set permissions of given file in Java? |
Concept :
We need to use below methods of File class that is present in java.io.* package.
- public boolean setReadable(boolean readable) - If readable is, true it sets the permission to allow read operations; if false it disallow read operations.
- public boolean setWritable(boolean writable) - If writable is, true it sets the permission to allow write operations; if false it disallow write operations.
- public boolean setExecutable(boolean executable) - If executable is, true it sets the permission to allow execute operations; if false it disallow execute operations.
Java Program :
Output :
Output - How to set file permission in Java? |
You might also like :
References :
https://docs.oracle.com/javase/7/docs/api/java/io/File.html#setReadable(boolean)
https://docs.oracle.com/javase/7/docs/api/java/io/File.html#setWritable(boolean)
https://docs.oracle.com/javase/7/docs/api/java/io/File.html#setExecutable(boolean)
https://docs.oracle.com/javase/7/docs/api/java/io/File.html#exists()
https://en.wikipedia.org/wiki/Regular_expression
Thank you friends, I hope you have clearly understood the solution of this problem. If you have any doubt, suggestion or query please feel free to comment below. You can also discuss this solution in our forum.
Tags : File Handling, set file permissions, if else, Scanner, String, File, setExecutable(boolean), setReadable(boolean), setWritable(boolean), exists(), Regular expression, java.io
How to change permissions(Read, Write and Execute) of given file in Java?
Reviewed by Rohit Agarwal
on
4/23/2017
Rating:
No comments:
Please provide your valuable comments. If you have any suggestion please share with me I will work on it and if you have any question or doubt please ask, don't hesitate. I am your friend, i will clarify all your doubts.