- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyFile.java
More file actions
Latest commit
34 lines (30 loc) · 1.12 KB
/
Copy pathCopyFile.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
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.FileNotFoundException;
importjava.io.IOException;
classCopyFile {
publicstaticvoidmain(String[] args) {
// character placeholder
inti;
// 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(FileInputStreamaFileIn = newFileInputStream(args[0]);
FileOutputStreamaFileOut = newFileOutputStream(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 (IOExceptionaExc) {
System.out.println("I/O Error: " + aExc);
}
}
}