try (FileOutputStream binFile =新FileOutputStream("data.dat");FileChannel binChannel = binFile.getChannel()) {
ByteBuffer buffer = ByteBuffer.allocate(100);
byte[] outputBytes = "Hello World!".getBytes();
buffer.put(outputBytes);
long int1Pos = outputBytes.length;
buffer.putInt(245);
binChannel.write(buffer);
java.io.RandomAccessFile ra = new java.io.RandomAccessFile("data.dat", "rwd");
FileChannel channel = ra.getChannel();
ByteBuffer readBuffer = ByteBuffer.allocate(100);
channel.position(int1Pos);
channel.read(readBuffer);
readBuffer.flip();
System.out.println("Int3 = " + readBuffer.getInt());
} catch(IOException e){
}
发布于 2018-09-28 00:25:50
您应该签出其中的Java文档。https://docs.oracle.com/javase/7/docs/api/java/nio/Buffer.html
翻转此缓冲区。在一系列通道读取或put操作之后,调用此方法为通道写入或相对get操作序列做准备。例如:
https://stackoverflow.com/questions/52541202
复制相似问题