Java is one of the most popular programming languages that provides extensive support to various functionalities like databases, sockets, etc. One such functionality is File Handling in Java. File Handling is necessary to perform various tasks on a file, such as reading, writing, etc. In this article, I will tell you what are the various file operations in Java.
What is File Handling in Java?
Java file handling implies reading from and writing data to a file. The File class from the java.io package, allows us to work with different formats of files. In order to use the File class, you need to create an object of the class and specify the filename or directory name.
Why File Handling is Required?
- File handling in java is an integral part of any programming language as file handling enables us to store the output of any particular program in a file and allows us to perform certain operations on it.
- In simple words, file handling means reading and writing data to a file.
Streams in Java
In Java, a sequence of data is known as a stream. This concept is used to perform I/O operations i.e, Input and Output on a file. There are two types of streams in this.
The Java InputStream class is the superclass of all input streams. The input stream is used to read data from numerous input devices like the keyboard, network, etc.
Reads one byte of data from the input stream.
Reads byte from the stream and stores that byte in the specified array.
It marks the position in the input stream until the data has been read.
Returns the number of bytes available in the input stream.
It checks if the mark() method and the reset() method is supported in the stream.
Returns the control to the point where the mark was set inside the stream.
Skips and removes a particular number of bytes from the input stream.
Closes the input stream.
The output stream is used to write data to numerous output devices like the monitor, file, etc. OutputStream is an abstract superclass that represents an output stream.
Writes the specified byte to the output stream.
Writes the bytes which are inside a specific array to the output stream.
Closes the output stream.
Forces to write all the data present in an output stream to the destination.
Based on the data type, there are two types of streams :
1. Byte Stream:
This stream is used to read or write byte data. The byte stream is again subdivided into two types which are as follows:
- Byte Input Stream: Used to read byte data from different devices.
- Byte Output Stream: Used to write byte data to different devices.
2. Character Stream:
This stream is used to read or write character data. Character stream is again subdivided into 2 types which are as follows:
- Character Input Stream: Used to read character data from different devices.
- Character Output Stream: Used to write character data to different devices.
In Java, with the help of File Class, we can work with files. This File Class is inside the java.io package. The File class can be used by creating an object of the class and then specifying the name of the file.
Entri provides video classes as well on various important topics by the excellent faculties. One will get revision modules, monthly tests based on the classes. You will be provided with an online platform to prepare for the exam. It also gives you access to clarify your doubts.