Check File Readability and Writability

JavaJavaBeginner
Practice Now

Introduction

In this lab, we will learn how to check if a file is readable or writable using Java programming language. We will use the File class provided in Java to determine the read and write properties of a file.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/FileandIOManagementGroup(["`File and I/O Management`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/FileandIOManagementGroup -.-> java/files("`Files`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/booleans("`Booleans`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/if_else("`If...Else`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117414{{"`Check File Readability and Writability`"}} java/classes_objects -.-> lab-117414{{"`Check File Readability and Writability`"}} java/class_methods -.-> lab-117414{{"`Check File Readability and Writability`"}} java/modifiers -.-> lab-117414{{"`Check File Readability and Writability`"}} java/oop -.-> lab-117414{{"`Check File Readability and Writability`"}} java/packages_api -.-> lab-117414{{"`Check File Readability and Writability`"}} java/files -.-> lab-117414{{"`Check File Readability and Writability`"}} java/identifier -.-> lab-117414{{"`Check File Readability and Writability`"}} java/arrays -.-> lab-117414{{"`Check File Readability and Writability`"}} java/booleans -.-> lab-117414{{"`Check File Readability and Writability`"}} java/data_types -.-> lab-117414{{"`Check File Readability and Writability`"}} java/if_else -.-> lab-117414{{"`Check File Readability and Writability`"}} java/operators -.-> lab-117414{{"`Check File Readability and Writability`"}} java/output -.-> lab-117414{{"`Check File Readability and Writability`"}} java/strings -.-> lab-117414{{"`Check File Readability and Writability`"}} java/system_methods -.-> lab-117414{{"`Check File Readability and Writability`"}} end

Create a Java file

Create a new Java file at ~/project/FileReadWrite.java using the command below:

touch ~/project/FileReadWrite.java

Then open the file and add the following code:

import java.io.File;

public class FileReadWrite {
    public static void main(String[] args) {

    }
}

This creates a new Java class named FileReadWrite, and we will add the code to check file readability and writability in the main method of this class.

Check file readability

Add the following code to check if a file is readable or not. We will use the canRead() method of the File class to determine whether a file is readable or not.

File file = new File("example.txt");

if(file.canRead()) {
    System.out.println("The file is readable.");
} else {
    System.out.println("The file is not readable.");
}

Save the file and run the following command to compile and run the program:

javac ~/project/FileReadWrite.java && java FileReadWrite

This code will check if the file named example.txt is readable or not. You can replace example.txt with the path of any file that you want to check.

Check file writability

Add the following code to check if a file is writable or not. We will use the canWrite() method of the File class to determine whether a file is writable or not.

File file = new File("example.txt");

if(file.canWrite()) {
    System.out.println("The file is writable.");
} else {
    System.out.println("The file is not writable.");
}

Save the file and run the following command to compile and run the program:

javac ~/project/FileReadWrite.java && java FileReadWrite

This code will check if the file named example.txt is writable or not. You can replace example.txt with the path of any file that you want to check.

Set file not writable

Add the following code to set a file not writable. We will use the setWritable(false) method of the File class to set the specified file to not writable.

File file = new File("example.txt");

if(file.setWritable(false)) {
    System.out.println("The file is not writable anymore.");
} else {
    System.out.println("Failed to set the file not writable.");
}

Save the file and run the following command to compile and run the program:

javac ~/project/FileReadWrite.java && java FileReadWrite

This code will set the specified file to not writable. You can replace example.txt with the path of any file that you want to set not writable.

Check file readability and writability at the same time

Add the following code to check if a file is readable and writable at the same time. We will use the canRead() and canWrite() methods of the File class to determine whether a file is readable and writable at the same time or not.

File file = new File("example.txt");

if(file.canRead() && file.canWrite()) {
    System.out.println("The file is both readable and writable.");
} else if(!file.canRead()) {
    System.out.println("The file is not readable.");
} else if(!file.canWrite()) {
    System.out.println("The file is not writable.");
}

Save the file and run the following command to compile and run the program:

javac ~/project/FileReadWrite.java && java FileReadWrite

This code will check if the file named example.txt is readable and writable at the same time. You can replace example.txt with the path of any file that you want to check.

Summary

In this lab, we have learned how to determine if a file is readable or writable in Java programming language using the File class. We have learned to use methods such as canRead(), canWrite(), and setWritable() to check the read and write properties of a file. By following the steps in this lab, you should now be able to check if a file is readable or writable using Java programming language.

Other Java Tutorials you may like