- Channel aggregation (gather) writes:
A clustered write ( gathering writes) refers to the "aggregation" of data in multiple Buffer to the Channel. Special Note: writes the data between position and limit to the Channel in the order of the buffers.
- Channel dispersion (scatter) reads:
Scatter read ( scattering Reads) refers to the data read from the Channel "scattered" into multiple Buffer . Special Note: The data read from the Channel is filled in sequence in the order of the buffers.
- Aggregate write ( gathering writes) and decentralized read ( scattering Reads) tests:
1 @Test2 Public voidTestgatherscatter ()throwsIOException {3Randomaccessfile Randomaccessfile =NewRandomaccessfile ("D:\\ce.txt", "RW");4 //Get channel5FileChannel channel =Randomaccessfile.getchannel ();6 7 //Request Cache Space8Bytebuffer byteBuffer00 = bytebuffer.allocate (128);9Bytebuffer ByteBuffer01 = bytebuffer.allocate (1024);Ten One //by Channel.read (bytebuffer[]bytebuffers), data is distributed to Bytebuffer00,bytebuffer01 -bytebuffer[] Bytebuffers ={byteBuffer00, byteBuffer01}; - Channel.read (bytebuffers); the - for(Bytebuffer bytebuffer:bytebuffers) { - Bytebuffer.flip (); - } + -System.out.println (NewString (Bytebuffers[0].array (), 0, Bytebuffers[0].limit ())); +System.out.println ("-----------------------------------------------------------"); ASystem.out.println (NewString (Bytebuffers[1].array (), 0, Bytebuffers[1].limit ())); at - //Aggregate Write -Randomaccessfile RandomAccessFile2 =NewRandomaccessfile ("D:\\ce_copy.txt", "WR"); - //1. Get the pipeline -FileChannel Channel2 =Randomaccessfile2.getchannel (); - in //2. Write data through a pipeline - Channel2.write (bytebuffers); to + channel2.close (); - channel.close (); the}
Java-nio (vi): Channel aggregation (gather) write and scatter (scatter) read