Tutorialspoint examples

Learn java tutorial with examples for beginners online

make a file read only java program

The setReadOnly() method of File class is used to make a file read only in java. It returns true is the operation is successful else return false.


package com.tutorialspointexamples;
import java.io.File;
public class ReadOnlyFileTest {
  public static void main(String args[]){
      try {
     	 File file = new File("D:/Test files/file 3.txt");
     	 //Convert the file read only
     	    System.out.println("File converted to readonly successfully");
     	    System.out.println("File not converted to readonly successfully");
      } catch (Exception e) {


Stored data in temporary file.
File converted to readonly successfully

Download this example.

Download tutorialspointexamples Android App
Copyright © 2018 Tutorialspoint examples DMCA.com Protection Status