Why Does BufferedReader Throw IOException in Java?
IOException is a type of checked exception which occurs during input/output operation. BufferedReader is used to read data from a file, input stream, database, etc. Below is the simplified steps of how a file is read using a BufferedReader in java.
- In RAM a buffered reader object is created.
- Some lines of a file are copied from secondary memory ( or Hard disk) and store in the buffer in the RAM.
- Now with the help of a buffered reader object our program can read the buffer in RAM.
- If all the lines are read then next some lines of the file are copied from secondary memory into the buffer.
This file system reading can fail at any time for many reasons. It may occur due to the file deleted or viruses in the file. Sometimes BufferedReader takes data from a network stream where the reading system can fail at any time.
So this type of error can occur in input operation when a BufferedReader is used. This is why a buffered reader throws IOException.
Below is an example of BufferedReader use
Input: a = 5, b = 3 Output: 8
If the file is deleted from the server-side while reading the input from the server-side, IOException is thrown.