Java File.setReadable(boolean readable, boolean ownerOnly) example

 Posted On  | Pooja Lakkihal 

In this java tutorial, You will learn how to set the owner's or everybody's read permission to read this abstract pathname file. Java File.setReadable() method returns true if and only if the operation succeeded.

Sets the owner's or everybody's read permission for this abstract pathname.

syntax

public boolean setReadable(boolean readable, boolean ownerOnly)

Parameters

readable - If true, sets the access permission to allow read operations; if false to disallow read operations.

ownerOnly - If true, the read permission applies only to the owner's read permission; otherwise, it applies to everybody.

Returns

true if and only if the operation succeeded. The operation will fail if the user does not have permission to change the access permissions of this abstract pathname. If readable is false and the underlying file system does not implement a read permission, then the operation will fail.

Throws

SecurityException - If a security manager exists and its SecurityManager.checkWrite(java.lang.String) method denies write access to the file.

Java File setReadable(boolean readable, boolean ownerOnly) example

  • Create File instance new File("C:\\technicalkeeda\\hello.txt");
  • file.setReadable(true, false) Set the file to readable by keeping true.
  • file.setReadable(false, false) Set false for not readable.
package com.technicalkeeda;

import java.io.File;

public class App {

    public static void main(String[] args) {

        File file = new File("C:\\technicalkeeda\\hello.txt");

        System.out.println(file + " :- Is this file set to readable ?  " + file.setReadable(true, false));

        System.out.println(file + " :- Is this file set to readable ?  " + file.setReadable(false, false));

    }

}

output

C:\technicalkeeda\hello.txt :- Is this file set to readable ?  true

C:\technicalkeeda\hello.txt :- Is this file set to readable ?  false


© technicalkeeda.com 2018

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0379 seconds.