How to change a file attribute to read only in java?

Below java code shows how to change file attribute to read only –

/****************************************************************************************
 * Created on 03-2012 Copyright(c) http://kodehelp.com All Rights Reserved.
 ****************************************************************************************/
package com.kodehelp.javaio;

import java.io.File;
import java.io.IOException;

/**
 * Created by http://kodehelp.com
 * Date: 03/05/2012
 */
public class ReadOnlyFileAttribute {
	public static void main(String[] args) throws IOException {
		File file = new File("sample.log");
        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");
        }
	}
}

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

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