以下是一个NIO的例子,这里比作是一个送货的比喻,一下文字简介:
1.送什么货物:FileInputStream fis = new FileInputStream("C:\\reset.css");
2.货物送到哪里:FileOutputStream fos = new FileOutputStream("D:\\reset.css");
3.需要接收货物的快递员:FileChannel fisChannel = fis.getChannel();
4.到达目的城市需要快递员送到手里:FileChannel fosChannel = fos.getChannel();
5.需要车辆来承载这些货物,定义这个车一下能装多少货物:ByteBuffer buffer = ByteBuffer.allocate(1024);
6.执行需要的元素,每次送完一车那么把这车标为空车:buffer.flip();
7.当装不满一车代表不用再接收和送了:if(num == -1)
8.通知快递员下班或者干其他的:fisChannel.close();fosChannel.close();