Skip to content

Commit d7f9137

Browse files
committed
Play with the AutomaticResourceManagement feature:)
1 parent b3171a0 commit d7f9137

3 files changed

Lines changed: 25 additions & 0 deletions

File tree

Chapter16/Copy.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.io.FileInputStream;
2+
import java.io.FileOutputStream;
3+
import java.io.FileNotFoundException;
4+
import java.io.IOException;
5+
6+
public class Copy{
7+
public static void main(String[] args) {
8+
if(args.length != 2){
9+
System.err.println("usage: java Copy srcFile srcFile");
10+
return;
11+
}
12+
// ARM, Java added automatic resource management feature implementation in below form.
13+
try(FileInputStream fis = new FileInputStream(args[0]);
14+
FileOutputStream fos = new FileOutputStream(args[1])){
15+
// resource usage block, you can follow it with catch and/or finally block.
16+
int b;
17+
while((b=fis.read())!=-1){
18+
fos.write(b);
19+
}
20+
}catch(IOException ioe){
21+
ioe.printStackTrace();
22+
};
23+
// No resource release tedious handle any more:)
24+
}
25+
}

Chapter16/CopyResult.png

27.9 KB
Loading

Chapter16/MulticatchStatement.png

27.9 KB
Loading

0 commit comments

Comments
 (0)