ByteBuffer的基本使用
使用Java NIO的API来读取文件。
1. 项目环境
maven项目中,引入以下依赖
xml
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.39.Final</version>
</dependency>
准备一个文本文件,其中的文本内容:
text
1234567890abc
2. 示例代码
案例1
java
public static void main(String[] args) {
try(FileInputStream fileInputStream = new FileInputStream("src/main/resources/Buffer01_HelloWorld.txt")) {
// 获取文件channel
FileChannel channel = fileInputStream.getChannel();
// 从内存中分配一个buffer,大小为10个字节
ByteBuffer byteBuffer = ByteBuffer.allocate(10);
// 从channel中读取数据,并写到buffer中
channel.read(byteBuffer);
// flip方法,切换到【读模式】;flip在这里的意思应该是【浏览 / 快速翻阅】
byteBuffer.flip();
// 打印buffer中的字节数据
// 当buffer中还有剩余数据,则不断读取
while (byteBuffer.hasRemaining()) {
// 每次只读一个字节
byte b = byteBuffer.get();
// 将字节强转成字符,打印
System.out.println((char) b);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
以上案例中
- 先获取文件数据读取通道:Channel;
- 再准备一个大小为10字节的缓存区 Buffer;
- 从Channel中读取数据,并写到buffer中;
- 再打印Buffer中的数据。
输出结果
text
"C:\Program Files\Java\jdk-1.8\bin\java.exe" ...
1
2
3
4
5
6
7
8
9
0
Process finished with exit code 0
从结果中可以看到,文件中的内容并没有全部读取出来,是因为只从文件中读取了一次,并且缓存的大小只有10;
要想读取文件中的全部内容,就要多次读取。
案例2
java
public static void main(String[] args) {
try(FileInputStream fileInputStream = new FileInputStream("src/main/resources/Buffer01_HelloWorld.txt")) {
// 获取文件channel
FileChannel channel = fileInputStream.getChannel();
// 从内存中分配一个buffer,大小为10个字节
ByteBuffer byteBuffer = ByteBuffer.allocate(10);
int read;
// read方法的返回值代表读取到的字节数,当返回值为-1时,代表文件中的所有数据读取完成
// 使用while一直读取,直到全部读取完
while ((read = channel.read(byteBuffer)) != -1) {
System.out.println("读到的字节数:" + read);
// flip方法,切换到【读模式】;flip在这里的意思应该是【浏览 / 快速翻阅】
byteBuffer.flip();
while (byteBuffer.hasRemaining()) {
byte b = byteBuffer.get();
System.out.println((char) b);
}
// buffer中的数据读取完成后,清空buffer的数据
byteBuffer.clear();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
以上案例
-
在案例一的基础上,多次从channel中读取数据到buffer,并且打印buffer中的数据,直到文件中的内容全部读取完;
-
buffer中的数据读取完成后,在 再次从channel中读取数据到buffer之前,要清空buffer的数据。
输出结果
jtext
"C:\Program Files\Java\jdk-1.8\bin\java.exe" ...
读到的字节数:10
1
2
3
4
5
6
7
8
9
0
读到的字节数:3
a
b
c
Process finished with exit code 0
可以看到一共读取了两次,实际上应该是3次,第一个读取10个字节,第二次读取剩余的3个字节,第三次没有读取到数据,read方法返回-1。