Answer to Question #291537 in Java | JSP | JSF for ram

Question #291537

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.


1
Expert's answer
2022-01-28T07:59:27-0500
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.");
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS