NIO分散読み取りおよび書き込みをコピーするための利用可能集まります

 

package com.atguigu.nio;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import org.junit.Test;

public class TestNio {

	@Test
	public void test() throws IOException {
		RandomAccessFile ranf = new RandomAccessFile("C:/Users/Administrator/Desktop/1.jpg", "r");
		FileChannel channel = ranf.getChannel();
		// 多个buffer
		ByteBuffer buff1 = ByteBuffer.allocate(10001);
		ByteBuffer buff2 = ByteBuffer.allocate(10001);

		ByteBuffer buff3 = ByteBuffer.allocate(10001);
       //将buff放在数组中
		ByteBuffer[] bus = new ByteBuffer[] { buff1, buff2, buff3 };
		//channel进行文件读取
		long read = channel.read(bus);
		for (ByteBuffer byteBuffer : bus) {
			byteBuffer.flip();
		}
		RandomAccessFile ran2 = new RandomAccessFile("C:/Users/Administrator/Desktop/12.jpg", "rw");
		//channel实现文件写入
		FileChannel channel2 = ran2.getChannel();
		channel2.write(bus);

	}
}

 

公開された17元の記事 ウォンの賞賛3 ビュー20000 +

おすすめ

転載: blog.csdn.net/qq_36547601/article/details/81506841