Java File.setWritable(boolean writable, boolean ownerOnly) example

 Posted On  | Pooja Lakkihal 

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

This method sets the owner's or everybody's write permission for this abstract pathname.

syntax

public boolean setWritable(boolean writable, boolean ownerOnly)

Parameters

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

ownerOnly - If true, the write permission applies only to the owner's write 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.

Throws

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

Java File setWritable(boolean writable, boolean ownerOnly) example

  • Create File instance new File("C:\\technicalkeeda\\hello.txt");
  • file.setWritable(true, false) Set the file to writable by keeping true.
  • file.setWritable(false, false) If you Set false file will not be able to write.
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 writable ?  " + file.setWritable(true, false));

        System.out.println(file + " :- Can you write in this file ?  " + file.canWrite());

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

        System.out.println(file + " :- Can you write in this file ?  " + file.canWrite());

    }

}

output

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

C:\technicalkeeda\hello.txt :- Can you write in this file ?  true

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

C:\technicalkeeda\hello.txt :- Can you write in this file ?  false


© technicalkeeda.com 2018

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0439 seconds.