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 :


package com.javamultiplex.FileHandling;

import java.io.File;
import java.util.Scanner;

/**
 * 
 * @author Rohit Agarwal
 * @category Problem Handling
 * @problem How to set new permissions of given file in Java?
 *
 */
public class SetFilePermissionInWindows {

 public static void main(String[] args) {

  Scanner input = null;
  try {
   input = new Scanner(System.in);
   System.out.println("Enter file name with extension : ");
   String fileName = input.nextLine();
   if (isValidFileName(fileName)) {

    //File is present in current working directory.
    File file = new File(fileName);
    if (file.exists()) {
     System.out.println("Before changing file permissions.");
     System.out.println("Executable : " + file.canExecute());
     System.out.println("Readable : " + file.canRead());
     System.out.println("Writable : " + file.canWrite());
     if (!file.canExecute()) {
      file.setExecutable(true);
     }
     if (!file.canRead()) {
      file.setReadable(true);
     }
     if (!file.canWrite()) {
      file.setWritable(true);
     }
     System.out.println("After changing file permissions.");
     System.out.println("Executable : " + file.canExecute());
     System.out.println("Readable : " + file.canRead());
     System.out.println("Writable : " + file.canWrite());

    } else {
     System.out.println("File is not present in current directory.");
    }
   } else {
    System.out.println("File name is not valid.");
   }
  } finally {
   if (input != null) {
    input.close();
   }
  }

 }

 private static boolean isValidFileName(String fileName) {

  String pattern = "^.+\\..+$";
  boolean result = false;
  if (fileName.matches(pattern)) {
   result = true;
  }
  return result;
 }

}



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.