Govt Exams
read() returns integer values of bytes. [65, 66, 67] are ASCII values for 'A', 'B', 'C'. Multiple read() calls return these values sequentially.
Filter streams are chained from inside-out. FileReader is wrapped with BufferedReader for better performance.
The readLine() method reads a line of text from the BufferedReader, returning a String without the newline character, or null if end of stream is reached.
FileNotFoundException (a subclass of IOException) is thrown when the specified file does not exist or cannot be opened.
The read() method returns an int. It returns the byte value (0-255) or -1 if end of stream is reached. This is why it needs to be cast to byte if needed.
The flush() method forces any buffered output to be written to the underlying stream immediately without closing it.
The read() method reads a single byte and returns it as an integer. Returns -1 if end of stream is reached.
FileReader is a character stream class that reads characters from a file. FileInputStream, DataInputStream, and BufferedInputStream are byte stream classes.
'throw' is a statement that explicitly throws an exception object. 'throws' is a clause in method signature indicating the method may throw specific checked exceptions. Example: throw new IOException(); vs public void method() throws IOException {}
try {
int x = 10/0;
} catch(ArithmeticException e) {
System.out.println("Caught");
} finally {
System.out.println("Finally");
}
System.out.println("After");
When ArithmeticException occurs, it's caught by the catch block printing 'Caught'. The finally block always executes, printing 'Finally'. Then normal program flow continues, printing 'After'.