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.
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.
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.
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.
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.
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.
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.
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.