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.
A
D
B
answer seems to be D. any one else has any comments.
The BufferedReader object that the br variable refers to is a try-with-resour c es structure resource, so the resource is released after leaving the try-with-resour c structure try block (the BufferedReader object c lose).
line n3 part in BufferedReader object is c then lose the call ready methods, it will throw IOEx c eption.
D – correct
The content is printed, after that the error is thrown on line n3:
java.io.IOException: Stream closed
where is the content
D. is correct
Employee 1
Employee 2
Employee 3
Exception in thread “main” java.io.IOException: Stream closed
at java.io.BufferedReader.ensureOpen(Unknown Source)
at java.io.BufferedReader.ready(Unknown Source)
at examen2.Test2.main(Test2.java:203) // line n3
Hello, to my knowledge the answer is B, going by the code in Brief me now,
Its hard to know, because there is no file with the content that, I’m thinking that we should assume that there is a file with the content the answer would be different?
How did you get your answer
Could you post your working code please
D
anyone with better answer than this.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/**
* Q6 Given the code fragment:
*/
public class Employee {
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;
}
} // End class
————————————————————————
Output:
run:
Exception in thread “main” java.io.FileNotFoundException: employee.txt (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.(FileInputStream.java:138)
at java.io.FileInputStream.(FileInputStream.java:93)
at java.io.FileReader.(FileReader.java:58)
at Employee.main(Employee.java:13) –>” This is line n1 ” <–
C:\Users\Chloe\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
Hello, to my knowledge the answer is B, going by the code in Brief me now,
Its hard to know, because there is no file with the content that, I’m thinking that we should assume that there is a file with the content the answer would be different?
D.
The code prints the content of the employee.txt file and throws an exception at line n3.