Java File.setWritable(boolean writable) example

 Posted On  | Pooja Lakkihal 

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

It set the owner's write permission for this abstract pathname.

syntax

public boolean setWritable(boolean writable)

Parameters

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

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 file.

Java File setWritable(boolean writable) example

  • Create File instance new File("C:\\technicalkeeda\\hello.txt");
  • file.setWritable(true) Set the file to writable by keeping true.
  • file.canWrite() Check if you can write in this file.
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));

        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


© technicalkeeda.com 2018

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0436 seconds.