Share on Google+Share on Google+

How to check a file is write-able or not in java

Advertisement
This tutorial demonstrate that how to rectify a file tha have write permission or not.

Description:

This example demonstrate how to check a file have write permission or not at a specified path.  The method canWrite() of the File class check for its write property of a file. As in the file path we see '\' (backslash) that is used to denote file separation but here in this example File.separator is used.  

Code:

import java.io.File;

public class CheckFileCanWrite {
  public static void main(String[] a) {
    File findFile = new File("C:" + File.separator + "java-tutorial"
        + File.separator + "Test" + File.separator +
        
"anyfile.txt");
    System.out.println(findFile.canWrite());
  }
}

Output:

The output of this example is either true or false depends on the file have write property at the specified path that is set in the program above.

Advertisements

Posted on: April 12, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.