FileChannel fc = new FileInputStream("data.txt").getChannel();ByteBuffer buff = ByteBuffer.allocate(1024);fc.read(buff);buff.flip();?while (buff.hasRemaining())?这里判断当前位置与限制为之间是否有元素,System.out.print((char) buff.ge
来源:学生作业帮助网 编辑:作业帮 时间:2024/07/06 12:47:59
![FileChannel fc = new FileInputStream(](/uploads/image/z/5635012-4-2.jpg?t=FileChannel+fc+%3D+new+FileInputStream%28%22data.txt%22%29.getChannel%28%29%3BByteBuffer+buff+%3D+ByteBuffer.allocate%281024%29%3Bfc.read%28buff%29%3Bbuff.flip%28%29%3B%3Fwhile+%28buff.hasRemaining%28%29%29%3F%E8%BF%99%E9%87%8C%E5%88%A4%E6%96%AD%E5%BD%93%E5%89%8D%E4%BD%8D%E7%BD%AE%E4%B8%8E%E9%99%90%E5%88%B6%E4%B8%BA%E4%B9%8B%E9%97%B4%E6%98%AF%E5%90%A6%E6%9C%89%E5%85%83%E7%B4%A0%2CSystem.out.print%28%28char%29+buff.ge)
FileChannel fc = new FileInputStream("data.txt").getChannel();ByteBuffer buff = ByteBuffer.allocate(1024);fc.read(buff);buff.flip();?while (buff.hasRemaining())?这里判断当前位置与限制为之间是否有元素,System.out.print((char) buff.ge
FileChannel fc = new FileInputStream("data.txt").getChannel();
ByteBuffer buff = ByteBuffer.allocate(1024);
fc.read(buff);
buff.flip();?
while (buff.hasRemaining())?这里判断当前位置与限制为之间是否有元素,
System.out.print((char) buff.get());
FileChannel fc = new FileInputStream("data.txt").getChannel();ByteBuffer buff = ByteBuffer.allocate(1024);fc.read(buff);buff.flip();?while (buff.hasRemaining())?这里判断当前位置与限制为之间是否有元素,System.out.print((char) buff.ge
楼主,给你简单分析下:
1、flip()
使缓冲区为一系列新的通道写入或相对获取 操作做好准备:它将限制(limit)设置为当前位置(position),然后将位置(position)设置为 0,将标记mark记为-1.
源码实现如下:
public final Buffer flip()
{
this.limit = this.position;
this.position = 0;
this.mark = -1;
return this;
}
2、hasRemaining()
判断当前位置是否在限制范围内,告知程序在当前位置和限制之间是否有元素,是否继续往下读取数据.
源码如下:
public final boolean hasRemaining()
{
return this.position < this.limit;
}
有问题欢迎提问,