netty/demo1/src/main/java/cn/bunny/file_dir/Demo01.java

34 lines
1007 B
Java

package cn.bunny.file_dir;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class Demo01 {
public static void main(String[] args) {
// 输入流
try (FileChannel channel = new FileInputStream("D:\\MyFolder\\Netty\\byteBuffter\\data.txt").getChannel()) {
// 准备缓冲区
ByteBuffer byteBuffer = ByteBuffer.allocate(10);
while (true) {
// 从channel 向buffer输入
int len = channel.read(byteBuffer);
if (len == -1) {
break;
}
// 输出buffer内容
byteBuffer.flip();
while (byteBuffer.hasRemaining()) {
byte b = byteBuffer.get();
System.out.println((char) b);
}
byteBuffer.clear();
}
} catch (IOException exception) {
}
}
}