How to check if a file is read-only in java

A read-only file is any file with the read-only attribute. Read-only files can be opened and accessed but you will not be able to make changes to it. You can make a file read-only by using setReadOnly() method and can check this using canWrite() method of File class. If the value return from the canWrite() method is false, means that file is in read only mode and you cannot modify it.


public class FileCheckReadonlyMode {
	public static void main(String[] args) {
		File fin = new File("c:/file.txt");

		boolean b = fin.canWrite();
		if (b == false) {
			System.out.println("File is in read only mode");
		} else {
			System.out.println("File is in not in read only mode");

You may also like...