Question 1: Write a program that uses java.io.BufferReader to read the lines from the text file that is stored on disk. Handle the exceptions with following two ways:
1. Using try-catch-finally block
2. By throwing it up the call stack to the caller method.
import java.io.BufferedReader;
import java.io.FileReader;
public class Main {
public static void readThrows() throws Exception {
BufferedReader reader = new BufferedReader(new FileReader("data.txt"));
String line;
while ((line = reader.readLine()) != null) {
}
reader.close();
}
public static void readCatch() {
try (BufferedReader reader = new BufferedReader(new FileReader("data.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
}
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("Finally.");
}
}
}
Comments
Leave a comment