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
- 
				horst1a
 - Posts: 37
 - Joined: Mon Jun 12, 2017 2:16 am
 - Contact:
 
- 
				admin
 - Site Admin
 - Posts: 10443
 - 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.
- 
				horst1a
 - 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: Bing [Bot] and 49 guests