How to check whether file is writable or file has write permission in java?

DeeKay August 27, 2011 at 6:32 pm · java.io

Below java code shows how to check whether file is writable or file has write permission –

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

import java.io.File;

/**
 * Created by http://kodehelp.com
 * Date: 8/27/11
 */
public class CanWriteFile {
    public static void main(String args[]){
        File file = new File("/demo.txt");
        System.out.println(file.canWrite() ? "is writable" : "is not writable");
    }
}
  • Nice tip but this can ┬ábe more easily done in 3 lines by using Guava library as shown in <a href=”http://javarevisited.blogspot.hk/2012/08/convert-inputstream-to-string-java-example-tutorial.html”>5 ways to convert InputStream to String in Java</a>