jbanana: Badly drawn banana (Default)
posted by [personal profile] jbanana at 05:49pm on 15/03/2021
You've been able to can auto-close a closable thing for ages now.
try(Reader r = new FileReader(/*blah*/)){
   r.whatever();
}
// r is automatically closed
But today I found that it works for something you already opened!
Reader r = new FileReader(/*blah*/);
try(r){
   r.whatever();
}
// r is automatically closed
This came up because I had a list of files and I wanted to close each one after handling it. So I couldn't create it in the header of the try block.

Links

March

SunMonTueWedThuFriSat
  1
 
2
 
3
 
4
 
5
 
6
 
7
 
8
 
9
 
10
 
11
 
12
 
13
 
14
 
15 16
 
17
 
18
 
19
 
20
 
21
 
22
 
23
 
24
 
25
 
26
 
27
 
28
 
29
 
30
 
31