-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCopyFile2.java
More file actions
29 lines (26 loc) · 831 Bytes
/
CopyFile2.java
File metadata and controls
29 lines (26 loc) · 831 Bytes
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
package chapter13;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/* A version of CopyFile that uses try with resources
It demonstrates two resources (in this case files) being
managed by single try statements
*/
public class CopyFile2 {
public static void main(String[] args) {
int i;
if (args.length != 2) {
System.out.println("Usage CopyFile2 File Name");
return;
}
try (FileInputStream fin = new FileInputStream(args[0]);
FileOutputStream fout = new FileOutputStream(args[1])) {
do {
i = fin.read();
if (i != -1) fout.write(i);
} while (i!=-1);
} catch (IOException e) {
System.out.println("I/O Error");
}
}
}