How to make a file read only in Java?

As a Java developer, you might have got a requirement to make a file read only in the Java program. When I started programming in Java, I didn’t know all the different ways to make a file read only. In this tutorial, I will list all ways to make a file read only.

Below are the different ways you can make a file read-only –

  1. java.io.File.setReadOnly()
  2. java.io.File.setWritable(false)
  3. PosixFilePermission
  4. Runtime.getRuntime().exec()

1. Using java.io.File.setReadOnly()


The attribute of a file can be changed to read-only by using the method java.io.File.setReadOnly(). This method does not require any parameters and it returns true if the file is set to read-only and false otherwise.

package com.kodehelp.java.io;
import java.io.File;
import java.io.IOException;
public class ReadOnlyFileAttribute {
    public static void main(String[] args) {
        File file = new File("kodehelp.txt");
        try {
            file.createNewFile();
            file.setReadOnly(); // Setting file attribute to Read only
            if (file.canWrite()){
                System.out.println("Writable File!");
            } else{
                System.out.println("Read only file");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2. Using java.io.File.setWritable()


java.io.File.setWritable(false) is a convenience method to set the owner’s write permission.

package com.kodehelp.java.io;
import java.io.File;
import java.io.IOException;
public class ReadOnlyFileAttribute {
    public static void main(String[] args) {
        File file = new File("kodehelp.txt");
        try {
            file.createNewFile();
            file.setWritable(false); // Mark it read only
            if (file.canWrite()){
                System.out.println("Writable File!");
            } else{
                System.out.println("Read only file");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

3. Using PosixFilePermission


Java 7 has introduced PosixFilePermission Enum and these PosixFilePermission can be converted into FileAttributes, which can be used while creating the file.

package com.kodehelp.java.io;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermissions;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Set;
public class ReadOnlyFileAttribute {
    public static void main(String[] args) {
        try {
            // File Path
            Path filePath = Paths.get("kodehelp.txt");
            // File permissions (Read only for USER, GROUP, and OTHER)
            Set<PosixFilePermission> permissions = PosixFilePermissions.fromString("r--r--r--");
            FileAttribute<Set<PosixFilePermission>> fileAttributes = PosixFilePermissions.asFileAttribute(permissions);
            // Create a file at the given file path with the given attributes
            Files.createFile(filePath, fileAttributes);
            File file = new File("kodehelp.txt");
            if (file.canWrite()){
                System.out.println("Writable File!");
            } else{
                System.out.println("Read only file");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

4. Using Runtime.getRuntime().exec()


The Runtime.getRuntime().exec() is platform-specific because of the system command we need to pass to it as parameter. All the above methods will work fine in most cases. Runtime.getRuntime().exec() executes the specified command and arguments in a separate process in the operating system.

4.1 Linux Approach

package com.kodehelp.java.io;
import java.io.File;
import java.io.IOException;
public class ReadOnlyFileAttribute {
    public static void main(String[] args) {
        try {
            File file = new File("kodehelp.txt");
            file.createNewFile();
            //Mark it read only in Linux/Unix
            Runtime.getRuntime().exec("chmod 444 "+ file.getAbsolutePath());
            if (file.canWrite()){
                System.out.println("Writable File!");
            } else{
                System.out.println("Read only file");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

4.2 Windows Approach

package com.kodehelp.java.io;
import java.io.File;
import java.io.IOException;
public class ReadOnlyFileAttribute {
    public static void main(String[] args) {
        try {
            File file = new File("kodehelp.txt");
            file.createNewFile();
            //Mark it read only in windows
            Runtime.getRuntime().exec("attrib " + "" + file.getAbsolutePath() + "" + " +R");
            if (file.canWrite()){
                System.out.println("Writable File!");
            } else{
                System.out.println("Read only file");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Hope these all methods help you make clear decision what method to use.

References: