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.


Java program that sets new permissions of given file.
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.

  1. public boolean setReadable(boolean readable) - If readable is, true it sets the permission to allow read operations; if false it disallow read operations.
  2. public boolean setWritable(boolean writable) - If writable is, true it sets the permission to allow write operations; if false it disallow write operations.
  3. 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 of Java program that sets new permissions of given file.
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

About Author:

I am simple guy with lot of ambitions. My main motive is to share whatever knowledge I have related to programming. With me you can easily learn how to solve any programming problem in Java.You can connect with me on social networking sites also.


Let's Get Connected: Linkedin | Facebook |

How to change permissions(Read, Write and Execute) of given file in Java? How to change permissions(Read, Write and Execute) of given file in Java? Reviewed by Rohit Agarwal on 4/23/2017 Rating: 5

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.

Powered by Blogger.