Home » Java programming language

Java File Class boolean canWrite() method with Example

Java File Class boolean canWrite() method: Here, we are going to learn about the boolean canWrite() method of File class with its syntax and example.
Submitted by Preeti Jain, on July 06, 2019

File Class boolean canWrite()

  • This method is available in package java.io.File.canRead().
  • This method is used to write the file and the file is represented by the abstract filepath or in other words this method is used to test whether the application can write the file or not.
  • The return type of this method is Boolean i.e. it returns true or false if true that means file can be written by the application which is represented by the filepath or in other words file already exists to write and returns false that means file does not exist that means the application is not allowed to write the file.
  • This method may raise an exception( i.e. Security Exception) if the write access is not given to the file.


    boolean canWrite(){


We don't pass any object as a parameter in the method of the File.

Return value:

The return type of this method is Boolean i.e. it returns true if the file already exists and allowed to write the file which is denoted by the abstract file path returns false otherwise.

Java program to demonstrate example of canWrite() method

// import the File class because we will use File class methods
import java.io.File;

// import the Exception class because it may 
// raise an exception when working with files
import java.lang.Exception;

public class WriteFile {
    public static void main(String[] args) {
        try {
            // Specify the path of file and we use double slashes 
            // to escape '\' character sequence for windows otherwise 
            // it will be considerable as url.
            File file1 = new File("C:\\Users\\computer clinic\\OneDrive\\Articles\\myjava.txt");
            File file2 = new File("C:\\Users\\computer clinic\\OneDrive\\Articles\\java.txt");

            // By using canWrite() is allowed to write the file 
            // if file is already exists and it returns true 
            // if file is writable else false returns.
            if (file1.canWrite())
                System.out.println("This file " + file1.getName() + " " + "is writable");
                System.out.println("This file " + file1.getName() + " " + "is not writable");

            // By using canWrite() is not allowed to write the file 
            // because this file is not already exists and it returns false.
            if (file2.canWrite())
                System.out.println("This file " + file2.getName() + " " + "is writable");
                System.out.println("This file " + file2.getName() + " " + "is not writable");
        } catch (Exception e) {
            System.out.println("An error occurred.");


D:\Programs>javac WriteFile.java
D:\Programs>java WriteFile

This file C:\Users\computer clinic\OneDrive\Articles\myjava.txt is not writable
This file C:\Users\computer clinic\OneDrive\Articles\java.txt is not writable

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.