File setReadOnly() method in Java with examples Last Updated : 28 Jan, 2019 Comments Improve Suggest changes Like Article Like Report The setReadOnly() method is a part of File class. The setReadOnly() function marks the specified file or directory such that only read operations are allowed on the file or directory. Function Signature: public boolean setReadOnly() Syntax: file.setReadOnly() Parameters: The function do not requires any parameters. Return Value: The function returns boolean data type. The function returns true if the File object could be set as Read Only else false. Exception: This method throws SecurityException if the method does not allow write access to the file Below programs will illustrate the use of setReadOnly() function: Example 1: Set existing file "F:\program.txt" to read only Java // Java program to demonstrate // the use of File.setReadOnly() method import java.io.*; public class GFG { public static void main(String args[]) { // create an abstract pathname (File object) File f = new File("F:\\program.txt"); // check if the file object // can be set as Read Only or not if (f.setReadOnly()) { // display that the file object // is set as Read Only or not System.out.println("File set as Read Only"); } else { // display that the file object // cannot be set as Read Only or not System.out.println("File cannot be set" + " as Read Only"); } } } Output: File set as Read Only Example 2: Set non existing file "F:\program1.txt" to read only Java // Java program to demonstrate // the use of File.setReadOnly() method import java.io.*; public class GFG { public static void main(String args[]) { // create an abstract pathname (File object) File f = new File("F:\\program1.txt"); // check if the file object // can be set as Read Only or not if (f.setReadOnly()) { // display that the file object // is set as Read Only or not System.out.println("File set as Read Only"); } else { // display that the file object // cannot be set as Read Only or not System.out.println("File cannot be set" + " as Read Only"); } } } Output: File cannot be set as Read Only The programs might not run in an online IDE. please use an offline IDE and set the path of the file Comment More infoAdvertise with us Next Article File setReadOnly() method in Java with examples A andrew1234 Follow Improve Article Tags : Java Java-Functions Java-IO package Java-File Class Practice Tags : Java Similar Reads File setLastModified() method in Java with Examples The setLastModified() method is a part of File class.The function sets the last modified time of the file or directory. The function sets the last modified value of the file in milliseconds.Function Signature:Â Â public boolean setLastModified(long time) Function Syntax:Â Â file.setLastModified(time) 3 min read Java FileInputStream read() Method with Examples FileInputStream class in Java is useful to read data from a file in the form of a sequence of bytes. FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader. The read() method of InputStream class reads a byte of data 3 min read Reader ready() method in Java with Examples The ready() method of Reader Class in Java is used to check whether this Reader is ready to be read or not. It returns a boolean which states if the reader is ready. Syntax: public void ready() Parameters: This method does not accepts any parameters Return Value: This method returns a boolean value 3 min read Optional get() method in Java with examples The get() method of java.util.Optional class in Java is used to get the value of this Optional instance. If there is no value present in this Optional instance, then this method throws NullPointerException. Syntax: public T get() Parameters: This method do not accept any parameter. Return value: Thi 2 min read Reader read() method in Java with Examples The read() method of Reader Class in Java is used to read a single character from the stream. This method blocks the stream till: It has taken some input from the stream. Some IOException has occurred It has reached the end of the stream while reading. This method is declared as abstract method. It 3 min read Like