File setWritable(boolean, boolean) in Java

setWritable(boolean, boolean): This method is available in the java.io.File class of Java.

Syntax:

boolean java.io.File.setWritable(boolean writable, boolean ownerOnly)

This method takes two arguments. This method sets the owner's or everybody's writing permission for this abstract pathname.

Parameters: Two parameters are required for this method.

writable: If true, sets the access permission to allow write operations; if false 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.

Throws:

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

Approach

Java

import java.io.File;

public class FilesetWritable2 {
    public static void main(String[] args) {

        String pathname = "D:\\hello.txt";
        File file = new File(pathname);

        boolean writable = true, ownerOnly = false;

        System.out.println(file.setWritable(
writable, ownerOnly));
    }
}

Output:

true


No comments:

Post a Comment