Consider the following code segment:
FileInputStream findings = new FileInputStream("log.txt"); DataInputStream dataStream = new DataInputStream(findings); BufferedReader br = new BufferedReader(new InputStreamReader(dataStream)); String line; while ((line = br.readLine()) != null) { System.out.println(line); } br.close();
Which two options are true regarding this code segment?
close()
statement will close only the BufferedReader object, and findings and dataStream
will remain unclosed.close()
statement will close the BufferedReader object and the underlying stream objects referred by findings and dataStream
.readLine()
method invoked in the statement br.readLine()
can throw an IOException; if this exception is thrown, br.close()
will not be called, resulting in a resource leak.readLine()
method invoked in the statement br.readLine()
can throw an IOException; however, there will not be any resource leaks since Garbage Collector collects all resources.close()
, so there is no possibility of resource leaks.B and C.
The br.close()
statement will close the BufferedReader object and the underlying stream objects referred to by findings and dataStream
.
The readLine()
method invoked in the statement br.readLine()
can throw an IOException;if this exception is thrown, br.close()
will not be called, resulting in a resource leak.
Note that Garbage Collector will only collect unreferenced memory resources; it is the programmer's responsibility to ensure that all other resources such as stream objects are released.