Given the code fragment:

public static void main(String[] args) throws IOException {
    BufferedReader brCopy = null;
    try (BufferedReader br = new BufferedReader(new FileReader("employee.txt"))) { // line n1
        br.lines().forEach(c -> System.out.println(c));
        brCopy = br;//line n2
    }
    brCopy.ready(); //line n3;
}

Assume that the ready method of the BufferedReader, when called on a closed BufferedReader, throws an exception, and employee.txt is accessible and contains valid text.

What is the result?

A. A compilation error occurs at line n3.
B. A compilation error occurs at line n1.
C. A compilation error occurs at line n2.
D. The code prints the content of the employee.txt file and throws an exception at line n3.

题解

br 变量所参考到的 BufferedReader 对象为 try-with-resources 结构的资源,因此在离开 try-with-resources 结构的 try 区块后,资源就会被释放(BufferedReader 对象会 close)。

line n3 的部份在 BufferedReader 对象被 close 之后调用 ready 方法,所以会抛出 IOException。