-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathChannelCopy.java
More file actions
36 lines (29 loc) · 848 Bytes
/
ChannelCopy.java
File metadata and controls
36 lines (29 loc) · 848 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
32
33
34
35
36
package io;
import java.nio.*;
import java.nio.channels.*;
import java.io.*;
import java.util.*;
/**
* RUN:
* javac io/ChannelCopy.java && java io.ChannelCopy io/ChannelCopy.java io/test.txt
*
* OUTPUT:
*
*/
public class ChannelCopy {
private static final int BSIZE = 1024;
public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.out.println("parameters: Sourse Target");
System.exit(1);
}
FileChannel in = new FileInputStream(args[0]).getChannel();
FileChannel out = new FileOutputStream(args[1]).getChannel();
ByteBuffer buffer = ByteBuffer.allocate(BSIZE);
while (in.read(buffer) != -1) {
buffer.flip();
out.write(buffer);
buffer.clear();
}
}
}