IO通道的使用

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
/**
*
* @author devin
* @date 2021-01-07 15:52
* @version 1.0.2
*/
public class ChanTest {

private static FileOutputStream fileOutputStream;
private static FileChannel fileChannel;

public static <buffer, buffer1, buffer2> void main(String[] args) throws IOException, InterruptedException {
fileOutputStream = new FileOutputStream("D://out.txt");
fileChannel = fileOutputStream.getChannel();

for(int i = 0;i < 10;i++){
Thread thread1 = new Thread(){

@Override
public void run() {
ByteBuffer buffer1 = ByteBuffer.wrap("123\r\n".getBytes());
try {
fileChannel.write(buffer1);
} catch (IOException e) {
e.printStackTrace();
}
}
};

Thread thread2 = new Thread(){

@Override
public void run() {
ByteBuffer buffer2 = ByteBuffer.wrap("654\r\n".getBytes());
try {
fileChannel.write(buffer2);
} catch (IOException e) {
e.printStackTrace();
}
}


};
thread1.start();
thread2.start();


}
Thread.sleep(3000);
fileChannel.close();
fileOutputStream.close();
}
}
Donate comment here