IO通道的使用
/** * * @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 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(); }}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~