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.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.