About Question enthuware.ocpjp.v7.2.1210 :
Posted: Wed Aug 23, 2017 6:24 am
Hello ,
I 've always thought, when using a try-with-resource statement the class has to implement AutoCloseable
but the following code compile without:
package food;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class IOTest {
public static void main(String[] args)
{ try(BufferedReader bfr = new BufferedReader(new FileReader("c:\\works\\a.java"))){
String line = null;
while( (line = bfr.readLine()) != null){
System.out.println(line); }
}catch(IOException e){
e.printStackTrace(); } } }
What happens to opened resources if class doesnt implement AutoCloseeable? Will they stay opened ?
I 've always thought, when using a try-with-resource statement the class has to implement AutoCloseable
but the following code compile without:
package food;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class IOTest {
public static void main(String[] args)
{ try(BufferedReader bfr = new BufferedReader(new FileReader("c:\\works\\a.java"))){
String line = null;
while( (line = bfr.readLine()) != null){
System.out.println(line); }
}catch(IOException e){
e.printStackTrace(); } } }
What happens to opened resources if class doesnt implement AutoCloseeable? Will they stay opened ?