Make file read only Java example shows how to make file read only in Java. The example also shows how to open, read and write read only file.
How to make a file read-only in Java?
Java File class provides various methods to handle files. Use the setReadOnly
method of the File class to make the file read-only.
1 |
public boolean setReadOnly() |
This method sets the file or directory denoted by the file object to read-only. It returns true if the operation was successful, false otherwise.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package com.javacodeexamples.ioexamples; import java.io.File; public class FileReadOnlyExample { public static void main(String[] args) { //path of the file you want to make read only String strFilePath = "C:/data/readme.txt"; //create file object File file = new File(strFilePath); /* * To make file read only, use setReadOnly method */ boolean success = file.setReadOnly(); System.out.println("Is file read only now? " + success); /* * You can also make directory read only using same method. */ String strDirPath = "C:/data"; File dir = new File(strDirPath); success = dir.setReadOnly(); System.out.println("Is directory read only now? " + success); } } |
Output
1 2 |
Is file read only now? true Is directory read only now? true |
Note: This method may throw the SecurityException
if there is no write access to the file.
How to check if the file is read-only or writable in Java?
Use the canWrite
method of the File class to check if the file or directory is read-only.
1 |
public boolean canWrite() |
This method returns true if the file or directory denoted by the file object can be written, false if there is no write permission to the file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.javacodeexamples.ioexamples; import java.io.File; public class FileReadOnlyExample { public static void main(String[] args) { String strFilePath = "C:/dir_1/data.txt"; //create file object File file = new File(strFilePath); boolean isWritable = file.canWrite(); if(isWritable) System.out.println("File is writable"); else System.out.println("File is read only"); } } |
Output
1 |
File is writable |
How to make a read-only file writable in Java?
You can use setWritable
method of the File class to make a read-only file writable in Java.
1 |
public boolean setWritable(boolean writable) |
This method sets the file or directory denoted by the file object to writable. It returns true if the operation was successful, false otherwise.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package com.javacodeexamples.ioexamples; import java.io.File; public class FileReadOnlyExample { public static void main(String[] args) { String strFilePath = "C:/dir_1/data.txt"; //create file object File file = new File(strFilePath); boolean isWritable = file.canWrite(); if(isWritable) System.out.println("File is writable"); else System.out.println("File is read only"); /* * To make read only file writable, use setWritable method */ file.setWritable(true); isWritable = file.canWrite(); if(isWritable) System.out.println("File is writable"); else System.out.println("File is read only") } } |
Output
1 2 |
File is read only File is writable |
Note: The setWritable
method is only available for Java 6 and later versions.
How to write a read-only file in Java?
If you try to write to a read-only file, it will throw the FileNotFoundException as given in the below example.
1 2 3 4 5 6 7 8 9 10 |
String strFilePath = "C:/dir_1/data.txt"; File file = new File(strFilePath); file.setReadOnly(); BufferedWriter bw = new BufferedWriter(new FileWriter(file)); bw.write("Hello World"); //should be in finally block bw.close(); |
Output
1 2 3 4 5 6 |
Exception in thread "main" java.io.FileNotFoundException: C:\dir_1\data.txt (Access is denied) at java.io.FileOutputStream.open(Native Method) at java.io.FileOutputStream.<init>(Unknown Source) at java.io.FileOutputStream.<init>(Unknown Source) at java.io.FileWriter.<init>(Unknown Source) at com.javacodeexamples.ioexamples.FileReadOnlyExample.main(FileReadOnlyExample.java:66) |
Before you start writing to the file, always make sure that it is writable. If it is not writable, use the setWritable
method to make the read-only file writable first.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
String strFilePath = "C:/dir_1/data.txt"; File file = new File(strFilePath); /* * Check if the file is writable first. If it is not, * set it to writable before writing to the file. */ if(!file.canWrite()){ System.out.println("File is read only. Making it writable"); file.setWritable(true); } BufferedWriter bw = new BufferedWriter(new FileWriter(file)); bw.write("Hello World"); System.out.println("String written to the file"); bw.close(); |
Output
1 2 |
File is read only. Making it writable String written to the file |
This example is a part of the Java File tutorial with examples.
Please let me know your views in the comments section below.