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 ?
About Question enthuware.ocpjp.v7.2.1210 :
Moderator: admin
-
- Posts: 37
- Joined: Mon Jun 12, 2017 2:16 am
- Contact:
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1210 :
Yes, exactly, you would have to close them explicitly in finally block. That is why try-with-resources/AutoCloseable was introduced in Java 7.horst1a wrote:Hello ,
What happens to opened resources if class doesnt implement AutoCloseeable? Will they stay opened ?
Should go through this tutorial : https://docs.oracle.com/javase/tutorial ... Close.html
HTH,
Paul.
-
- Posts: 37
- Joined: Mon Jun 12, 2017 2:16 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1210 :
Thanx, Paul,
thats what i thought would happen
thats what i thought would happen
Who is online
Users browsing this forum: No registered users and 33 guests