public class Test {
public static void main(String[] args) {
try (FileChannel channel = new FileInputStream("test.txt").getChannel()) {
ByteBuffer byteBuffer = ByteBuffer.allocate(10);
while (true){
int read = channel.read(byteBuffer);
if(read == -1){
break;
}
byteBuffer.flip();
while (byteBuffer.hasRemaining()){
byte b = byteBuffer.get();
System.out.println("读取到的数据为:" + (char)b);
}
byteBuffer.clear();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
test.txt中内容
1234567890abc