IO通道的使用 Posted on 2021-02-13 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253/** * * @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 Donate WeChat Pay