This write-up will provide a profound understanding of the following concepts regarding file handling in java:
- File Handling in java
- How to Work with File Class
- I/O Operations in java
- File Handling Methods
So, let’s get started!
What is File Handling
In java, there exists a class named “File” that belongs to the “java.io” package, allowing us to deal with various file formats. File handling in java refers to a procedure that allows us to read data from a file and write data to a file.
How to Work with File Class
In order to work with File class, the first thing that we need to do is “import” the file class using “import” keyword as shown in the below-given snippet:
Importing the File class enables us to create the object of that class and the proper way of creating an object is shown in the following snippet:
The name of the file you want to access, will be specified within the parenthesis.
What are I/O Operations in java
To perform input/output operations on files, java utilizes the concept of streams. So, let’s understand what are streams in java?
Streams
Java provides a concept of streams which is nothing but a sequence of data and it can be either byte stream, or character stream. As the name itself suggests, the byte streams are used to work with byte data while the character streams can be used to work with the characters.
Let’s move one step further to understand the concept of File Handling methods that can be used to perform various operations on the files such as file creation, deletion, etc.
File Handling Methods in Java
In java, the File class provides several file handling methods that can be utilized to achieve different functionalities for example the createNewFile(), mkdir() methods are used to create the file and directory respectively. The list of some frequently used file handling methods is provided in the below-given table:
Method Name | Description |
---|---|
createNewFile() | Boolean type method used to create an empty file. |
mkdir() | Boolean type method that creates a directory. |
delete() | Boolean type method that deletes a file. |
getName() | String type method used to get the file name. |
getAbsolutePath() | String type method used to get the file path. |
list() | String type method used to get the array of files within a directory. |
canRead() | Boolean type method that checks whether the file is readable or not. |
canWrite() | Boolean type method that checks whether the file is writable or not. |
exists() | Boolean type method used to check whether specified file exists. |
length() | Long type method used to get the size of file in Bytes. |
All these methods can be utilized with the object of file class to perform several file handling operations. For the clarity of concepts let’s implement some of the above-mentioned methods practically:
createNewFile() method
In order to create a file the createNewFile() method can be used.
Example
The below code snippet provides detailed understanding of how to utilize the createNewFile() method to create a file:
public static void main(String[] args) {
try {
File fileObj = new File("C:FileHandlingExample.txt");
if (fileObj.createNewFile()) {
System.out.println("File created: " + fileObj.getName());
} else {
System.out.println("File Exists Already");
}
} catch (IOException excep) {
System.out.println("Error");
excep.printStackTrace();
}
}
}
In the above-given snippet we created the object of the File class and within parentheses, we specified the file name and path. Afterward, there are three possibilities: file created successfully, file already exists or an Error occurs so for that we utilized the concept of try-catch to handle the exceptions:
The output verifies the working of createNewFile() method as it succeeds to create a file.
delete() method
The File class provides another useful method known as the delete() method that can be used to delete a specific file.
Example
In this example we will delete a file named “FileHandlingExample.txt” using delete() method:
The below-given screenshot will provide a detailed understanding of the delete() method:
Similarly, you can utilize the other methods to perform different functionalities depending upon your needs.
Conclusion
In java, file handling is nothing but a process of reading the data from a file and writing the data to a file. Java provides a predefined class named “File” that assists us in performing any kind of operations on a file. To avail the functionalities of the File class, there is a need to import the File Class using the import keyword and once the File class is imported then any of its methods can be used to achieve various functionalities such as file creation, deletion, getting file information and so on. This write-up provides a comprehensive overview of file handling where it explains what is file handling, methods to and how to work with files.