Wednesday, October 8, 2025
HomeLanguagesJavaFile setWritable() method in Java with Examples

File setWritable() method in Java with Examples

The setWritable() method is a part of File class.The function sets the owner’s or everybody’s permission to Write the abstract pathname. The function is a overloaded function. One function requires two parameters and the other only one.

Function Signature:

public boolean setWritable(boolean a, boolean b)
public boolean setWritable(boolean a)

Function Syntax:

file.setWritable(boolean a, boolean b)
file.setWritable(boolean a)

Parameters: The function is a overloaded function:

  • For the first overload:
    • If a true value is passed as the first parameter then it is allowed to Write the abstract pathname, else it is not allowed to Write the file.
    • If a true value is passed as the second parameter then the Write permission applies to the owner only, else it applies to everyone

Return value: This function returns a boolean value which states whether the operation succeeded or not.

Exception: This method throws Security Exception if the function is not allowed write access to the file.

Below programs will illustrate the use of the setWritable() function

Example 1: We will try to change the setWritable permission of owner of a existing file in f: directory.




// Java program to demonstrate the
// use of setWritable() function
  
import java.io.*;
  
public class solution {
    public static void main(String args[])
    {
  
        // try catch block to handle exceptions
        try {
  
            // Create a file object
            File f = new File("f:\\program.txt");
  
            // Check if the Writable permission
            // can be set to new value
            if (f.setWritable(true)) {
  
                // Display that the Writable permission
                // is be set to new value
                System.out.println("Writable permission is set");
            }
            else {
  
                // Display that the Writable permission
                // cannot be set to new value
                System.out.println("Writable permission cannot be set");
            }
        }
        catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }
}


Output:

Writable permission is set

Example 2: We will try to change the setWritable permission of everyone of a existing file in f: directory.




// Java program to demonstrate the
// use of setWritable() function
  
import java.io.*;
  
public class solution {
    public static void main(String args[])
    {
  
        // try-catch block to handle exceptions
        try {
  
            // Create a file object
            File f = new File("f:\\program.txt");
  
            // Check if the Writable permission
            // can be set to new value
            if (f.setWritable(true, false)) {
  
                // Display that the Writable permission
                // is be set to new value
                System.out.println("Writable permission"
                                   + " is set");
            }
            else {
  
                // Display that the Writable permission
                // cannot be set to new value
                System.out.println("Writable permission"
                                   + " cannot be set");
            }
        }
        catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }
}


Output:

Writable permission is set

The programs might not run in an online IDE. please use an offline IDE and set the Parent file of the file

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32342 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6712 POSTS0 COMMENTS
Nicole Veronica
11875 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11937 POSTS0 COMMENTS
Shaida Kate Naidoo
6833 POSTS0 COMMENTS
Ted Musemwa
7092 POSTS0 COMMENTS
Thapelo Manthata
6786 POSTS0 COMMENTS
Umr Jansen
6789 POSTS0 COMMENTS