ByteBuffer的allocate()方法详解
allocate()方法用于分配缓冲区。但是如果是聚集写入,与分散读取,就需要注意这个大小设置,
String property = System.getProperty("user.dir");
FileOutputStream FileOutputStream = new FileOutputStream(property + "/lib/aab.text");
FileChannel channel = FileOutputStream.getChannel();
ByteBuffer allocate1 = ByteBuffer.allocate(90);
ByteBuffer allocate2 = ByteBuffer.allocate(400);
allocate1.asCharBuffer().put("400");
allocate2.asCharBuffer().put("The world is so big, I want to see it.");
channel.write(new ByteBuffer[]{
allocate1,allocate2});
channel.close();
这里有两个buffer,分别分配缓冲区90字节,400字节,下面读取输出:
String property = System.getProperty("user.dir")