With Lombok
01 import lombok.Cleanup;
02 import java.io.*;
03
04 public class CleanupExample {
05 public static void main(String[] args) throws IOException {
06 @Cleanup InputStream in = new FileInputStream(args[0]);
07 @Cleanup OutputStream out = new FileOutputStream(args[1]);
08 byte[] b = new byte[10000];
09 while (true) {
10 int r = in.read(b);
11 if (r == -1) break;
12 out.write(b, 0, r);
13 }
14 }
15 }
Vanilla Java
01 import java.io.*;
02
03 public class CleanupExample {
04 public static void main(String[] args) throws IOException {
05 InputStream in = new FileInputStream(args[0]);
06 try {
07 OutputStream out = new FileOutputStream(args[1]);
08 try {
09 byte[] b = new byte[10000];
10 while (true) {
11 int r = in.read(b);
12 if (r == -1) break;
13 out.write(b, 0, r);
14 }
15 } finally {
16 if (out != null) {
17 out.close();
18 }
19 }
20 } finally {
21 if (in != null) {
22 in.close();
23 }
24 }
25 }
26 }