-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyFile2.java
More file actions
31 lines (26 loc) · 959 Bytes
/
CopyFile2.java
File metadata and controls
31 lines (26 loc) · 959 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
30
31
// implement the C-lang binary 'cp' in Java
// (slightly different version, based on the documentation from Oracle)
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
class CopyFile2 {
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("Wrong number of arguments.");
System.out.println("Usage: CopyFile <file_in> <file_out>");
return;
}
// character placeholder
int c;
try(FileInputStream aFileIn = new FileInputStream(args[0]);
FileOutputStream aFileOut = new FileOutputStream(args[1])) {
// not 'do...while'!
while ((c = aFileIn.read()) != -1)
aFileOut.write(c);
} catch (IOException aExc) {
System.out.println("I/O Error: " + aExc);
aExc.printStackTrace();
}
}
}