Tutorialspoint examples

Learn java tutorial with examples for beginners online

make a read only file writable java program


The setWritable() method of File class is used to make a read only file writable in java. It takes a boolean value as a parameter.

Example:

package com.tutorialspointexamples;
 
import java.io.File;
 
public class WritableFileTest {
  public static void main(String args[]){
      try {
    	 //File
     	 File file = new File("D:/Test files/newTest.txt");
     	 //Check the file is writable or read only
     	 if(file.canWrite()){
     	    System.out.println("File is writable.");
         }else{
     	    System.out.println("File is read only.");
     	 }
     	//changing the file mode to writable
     	file.setWritable(true);
     	//Check the file is writable or read only
    	 if(file.canWrite()){
    	    System.out.println("File is writable.");
        }else{
    	    System.out.println("File is read only.");
    	 }
      } catch (Exception e) {
          e.printStackTrace();
      } 
  }
}

Output:

File is read only.
File is writable.

Download this example.

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