Change a file attribute to writable in Java

Description

The following code shows how to change a file attribute to writable.

Example


//from ww w .  j  a  va  2  s  .com
import java.io.File;

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

    if (file.canWrite()) {
      System.out.println("File is writable!");
    } else {
      System.out.println("File is in read only mode!");
    }
    file.setWritable(true);
    if (file.canWrite()) {
      System.out.println("File is writable!");
    } else {
      System.out.println("File is in read only mode!");
    }
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    I/O »




Binary File
Byte Array
CharSet
Checksum
Console
Create Copy Move Delete
Directory
Drive
Encode Decode
File Attribute
File Lock
File System
GZIP
Jar File
NIO Buffer
Path
Scanner
StreamTokenizer
Temporary File
Text File
Zip