File setReadOnly() in Java

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

Syntax:

boolean java.io.File.setReadOnly()

This method marks the file or directory named by this abstract pathname so that only read operations are allowed.

Note: After invoking this method the file or directory will not change until it is either deleted or marked to allow write access.

Parameters: NA

Returns: true if and only if the operation succeeded; false otherwise.

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 FilesetReadOnly {
    public static void main(String[] args) {

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

        System.out.println(file.setReadOnly());
    }
}

Output:

true


No comments:

Post a Comment