Given:
private static void copyContents() {
try (
InputStream fis = new FileInputStream("report1.txt");
OutputStream fos = new FileOutputStream("consolidate.txt");
) {
byte[] buf = new byte[8192];
int i;
while ((i = fis.read(buf)) != -1) {
fos.write(buf, 0, i);
}
fis.close();
fis = new FileInputStream("report2.txt");
while ((i = fis.read(buf)) != -1) {
fos.write(buf, 0, i);
}
What is the result?
A.
Compilation fails due to an error at line 28
B.
Compilation fails due to error at line 15 and 16
C.
The contents of report1.txt are copied to consolidate.txt. The contents of report2.txt are
appended to consolidate.txt, after a new line
D.
The contents of report1.txt are copied to consolidate.txt. The contents of report2.txt are
appended to consolidate.txt, without a break in the flow.
Explanation:
The auto-closable resource fis may not be assigned.
Note: The try-with-resources statement is a try statement that declares one or more resources. A
resource is an object that must be closed after the program is finished with it. The try-withresources 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 implement
java.io.Closeable, can be used as a resource.
Reference: The Java Tutorials,The try-with-resources Statement
Can’t indentify the line numbers of the code