-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChannelCopy.java
More file actions
24 lines (21 loc) · 840 Bytes
/
ChannelCopy.java
File metadata and controls
24 lines (21 loc) · 840 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
package btp.oneP;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class ChannelCopy {
private static final int BSIZE = 1024;
public static void main(String[] args) throws IOException {
FileChannel in = new FileInputStream("C:\\Users\\dell\\Desktop\\IO\\channelIn.txt").getChannel(),
out = new FileOutputStream("C:\\Users\\dell\\Desktop\\IO\\channelOut.txt").getChannel();
ByteBuffer buffer = ByteBuffer.allocate(BSIZE);
while(in.read(buffer) != -1){
//写完之后:limit = capacity;position = 实际数据大小
buffer.flip();//limit->position;position->0
out.write(buffer);
buffer.clear();//limit->capacity;position->0;准备下一路的读
}
}
}