Java.io.File.setWritable() Method
Description
The java.io.File.setWritable(boolean writable, boolean ownerOnly) method to sets the owner's or everybody's write permission for this abstract pathname.
Declaration
Following is the declaration for java.io.File.setWritable(boolean writable, boolean ownerOnly) method:
public boolean setWritable(boolean writable, boolean ownerOnly)
Parameters
writable -- If true, allows the write access permission; if false, the write access permission is disallowed.
ownerOnly -- If true, the write access permission applies only to the owner, else it applies to everybody.
Return Value
The method returns true if and only if the operation succeeded.
Exception
SecurityException -- If a security manager exists and its method denies write access to the file.
Example
The following example shows the usage of java.io.File.setWritable(boolean writable, boolean ownerOnly) method.
package com.tutorialspoint;
import java.io.File;
public class FileDemo {
public static void main(String[] args) {
File f = null;
boolean bool = false;
try{
// create new File object
f = new File("C:/test.txt");
// returns true if file exists
bool = f.exists();
// if file exists
if(bool)
{
// set file access to writable for everybody
bool = f.setWritable(true, false);
// print
System.out.println("setWritable() succeeded?: "+bool);
// checks whether the file is writable
bool = f.canWrite();
// prints
System.out.print("Is file writable?: "+bool);
}
}catch(Exception e){
// if any error occurs
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result:
setWritable() succeeded?: true Is file writable?: true