Java.io.File.setWritable() Method

Advertisements


Description

The java.io.File.setWritable(boolean writable) method to set the owner's write permission for this abstract pathname.

Declaration

Following is the declaration for java.io.File.setWritable(boolean writable) method:

public boolean setWritable(boolean writable)

Parameters

  • writable -- If true, allows the write access permission; if false, the write access permission is disallowed.

Return Value

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) 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
            bool = f.setWritable(true);
            
            // 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



Advertisements
Advertisements