File Operations in Java 11
File Operations in Java 11
File Operations in Java 11
In Java, a File is an abstract data type. A named location used to store related information
is known as a File. There are several File Operations like creating a new File, getting
information about File, writing into a File, reading from a File and deleting a File
Before understanding the File operations, it is required that we should have knowledge
of Stream and File methods. If you have knowledge about both of them, you can skip it.
Stream
A series of data is referred to as a stream. In Java, Stream is classified into two types,
i.e., Byte Stream and Character Stream.
Byte Stream
Byte Stream is mainly involved with byte data. A file handling process with a byte stream is a
process in which an input is provided and executed with the byte data.
Character Stream
Character Stream is mainly involved with character data. A file handling process with a
character stream is a process in which an input is provided and executed with the character data.
4. exists() Boolean The exists() method is used to check whether the spec
not.
6. getName() String The getName() method is used to find the file name.
8. length() Long The length() method is used to get the size of the file in
9. list() String[] The list() method is used to get an array of the files avai
10. mkdir() Boolean The mkdir() method is used for creating a new directory
File Operations
We can perform the following operation on a file:
o Create a File
o Get File Information
o Write to a File
o Read from a File
o Delete a File
Create a File
Create a File operation is performed to create a new file. We use the createNewFile() method
of file. The createNewFile() method returns true when it successfully creates a new file and
returns false when the file already exists.
CreateFile.java
Output:
Explanation:
In the above code, we import the File and IOException class for performing file operation and
handling errors, respectively. We create the f0 object of the File class and specify the location of
the directory where we want to create a file. In the try block, we call
the createNewFile() method through the f0 object to create a new file in the specified location.
If the method returns false, it will jump to the else section. If there is any error, it gets handled in
the catch block.
Let's take an example to understand how to use file methods to get the information of the file.
FileInfo.java
Output:
Description:
In the above code, we import the java.io.File package and create a class FileInfo. In the main
method, we create an object of the text file which we have created in our previous example. We
check the existence of the file using a conditional statement, and if it is present, we get the
following information about that file:
Write to a File
The next operation which we can perform on a file is "writing into a file". In order to write
data into a file, we will use the FileWriter class and its write() method together. We need to
close the stream using the close() method to retrieve the allocated resources.
Let's take an example to understand how we can write data into a file.
WriteToFile.java
Output:
Explanation:
If we get any error in the try section, it jumps to the catch block. In the catch block, we
handle the IOException and print a custom message.