-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCopy.java
More file actions
47 lines (44 loc) · 1.04 KB
/
Copy pathCopy.java
File metadata and controls
47 lines (44 loc) · 1.04 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
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Copy{
public static void main(String[] args){
if(args.length != 2){
System.err.println("usage: java Copy srcFile destFile");
return;
}
FileChannel inChannel = null;
FileChannel outChannel = null;
try{
FileInputStream fis = new FileInputStream(args[0]);
FileOutputStream fos = new FileOutputStream(args[1]);
inChannel = fis.getChannel();
outChannel = fos.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(128);
while(inChannel.read(buffer) != -1){
buffer.flip();
outChannel.write(buffer);
buffer.compact();
}
}catch(IOException ioe){
ioe.printStackTrace();
}finally{
if(inChannel!=null){
try{
inChannel.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
if(outChannel != null){
try{
outChannel.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}
}
}