Which statement is true?

Given the code fragment:
<code>
public static void processFile () throws IOException {
Try (FileReader fr = new FileReader (“logfilesrc.txt”);
FileWriter fw = new FileWriter (“logfilesdst.txt”) ) {
int i = fr.read();
}
</code>
Which statement is true?

Given the code fragment:

public static void processFile () throws IOException {
Try (FileReader fr = new FileReader ("logfilesrc.txt");
FileWriter fw = new FileWriter ("logfilesdst.txt") ) {
int i = fr.read();
}

Which statement is true?

A.
The code fragment contains compilation errors.

B.
The java runtime automatically closes the FileWriter Instance first and the FileReader instance
next.

C.
The java runtime automatically closes the FileReader Instance first and the FileWriter instance
next.

D.
The developer needs to close the FileReader instance first and the FileWriter instance explicitly
in a catch block.

E.
The Java runtime may close the FileReader and FileWriter instance in an intermediate manner.
Developers should not rely on the order in which they are closed.

Explanation:
The try-with-resources statement is a try statement that declares one or more
resources. Aresource is an object that must be closed after the program is finished with it. The trywith-resources statement ensures that each resource is closed at the end of the statement. Any
object that implements java.lang.AutoCloseable, which includes all objects which
implementjava.io.Closeable, can be used as a resource.
Reference: The Java Tutorials,The try-with-resources Statement



Leave a Reply 0

Your email address will not be published. Required fields are marked *