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();
}
}
复制代码
NIO
程序员文章站
2022-04-23 23:43:15
...
上一篇: NIO