-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCopyFile.java
More file actions
59 lines (52 loc) · 1.54 KB
/
CopyFile.java
File metadata and controls
59 lines (52 loc) · 1.54 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package chapter13;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/*
Copy a file,
To use this program specify the name
of the source file and destination file.
For example,to copy a file calles FIRST.txt
to a file called SECOND.txt
use the following command line
java CopyFile FIRST.txt SECOND.txt
*/
public class CopyFile {
public static void main(String[] args) {
int i;
FileInputStream fin = null;
FileOutputStream fout = null;
//First confirm that both files have been specified
if (args.length != 2) {
System.out.println("Usage : CopyFile from to");
return;
}
//Copy a file
try {
//Attempt to open the files
fin = new FileInputStream(args[0]);
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");
}
finally {
try {
if(fin!=null)fin.close();
} catch (IOException e) {
System.out.println("Error closing input file");
}
try {
if(fout!=null)fout.close();
} catch (IOException e) {
System.out.println("Error closing output file");
}
}
}
}