欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

NIO

程序员文章站 2022-04-23 23:43:15
...
package com.zml.nio.test;

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

import org.junit.Test;


public class ChannelTest {

	@Test
	public void test() throws IOException {
		RandomAccessFile file = new RandomAccessFile("D:\\abc.txt", "rw");
		FileChannel channel = file.getChannel();
		ByteBuffer buf =ByteBuffer.allocate(48);
		int read = channel.read(buf);
		while(read!=-1){
			System.out.println("read:"+read);
			buf.flip();
			while(buf.hasRemaining()){
				System.out.println("remaining:"+(char)buf.get());
			}
			buf.clear();
			read = channel.read(buf);
		}
		file.close();
		
	}
}
复制代码