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.
Learn Coding in your Language! Enroll Here!
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.
Input Stream
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.
S No. | Method | Description |
1 | read() |
Reads one byte of data from the input stream.
|
2 | read(byte[] array)() |
Reads byte from the stream and stores that byte in the specified array.
|
3 | mark() |
It marks the position in the input stream until the data has been read.
|
4 | available() |
Returns the number of bytes available in the input stream.
|
5 | markSupported() |
It checks if the mark() method and the reset() method is supported in the stream.
|
6 | reset() |
Returns the control to the point where the mark was set inside the stream.
|
7 | skips() |
Skips and removes a particular number of bytes from the input stream.
|
8 | close() |
Closes the input stream.
|
Learn to code from industry experts! Enroll here
Output 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.
S. No. | Method | Description |
1 | write() |
Writes the specified byte to the output stream.
|
2 | write(byte[] array) |
Writes the bytes which are inside a specific array to the output stream.
|
3 | close() |
Closes the output stream.
|
4 | flush() |
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.