How can I change file attribute to writable?

Prior to Java 1.6 the java.io.File class doesn’t include a method to change a read only file attribute and make it writable. To do this on the old days we have to utilize or called operating system specific command. In Java 1.6 a new method named setWritable() was introduced to do exactly what the method name says.

package org.kodejava.io;

import java.io.File;

public class WritableExample {
    public static void main(String[] args) throws Exception {
        File file = new File("Writable.txt");

        // Create a file only if it doesn't exist.
        boolean created = file.createNewFile();

        // Set file attribute to read only so that it cannot be written
        boolean succeeded = file.setReadOnly();

        // We are using the canWrite() method to check whether we can
        // modified file content.
        if (file.canWrite()) {
            System.out.println("File is writable!");
        } else {
            System.out.println("File is in read only mode!");
        }

        // Now make our file writable
        succeeded = file.setWritable(true);

        // re-check the read-write status of file
        if (file.canWrite()) {
            System.out.println("File is writable!");
        } else {
            System.out.println("File is in read only mode!");
        }
    }
}

And here are the result of the code snippet above:

File is in read only mode!
File is writable!
Wayan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.