-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyFile.java
More file actions
34 lines (30 loc) · 1.12 KB
/
CopyFile.java
File metadata and controls
34 lines (30 loc) · 1.12 KB
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
32
33
34
// implement the C-lang binary 'cp' in Java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
class CopyFile {
public static void main(String[] args) {
// character placeholder
int i;
// check command-line argument number
if (args.length != 2) {
System.out.println("Wrong number of arguments.");
System.out.println("Usage: CopyFile <file_in> <file_out>");
return;
}
// buffer the input file and write to output file
// use the 'try' context manager
try(FileInputStream aFileIn = new FileInputStream(args[0]);
FileOutputStream aFileOut = new FileOutputStream(args[1])) {
do {
// possible to do in a single line without buffering
// but that removes the extra -1 sanity check
i = aFileIn.read();
if (i != -1) aFileOut.write(i);
} while (i != -1);
} catch (IOException aExc) {
System.out.println("I/O Error: " + aExc);
}
}
}