尝试(FileOutputStream binFile = new 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){
}
答案 0 :(得分:1)
您应该在其上签出Java文档。 https://docs.oracle.com/javase/7/docs/api/java/nio/Buffer.html
翻转此缓冲区。将限制设置为当前位置,然后将该位置设置为零。如果定义了标记,则将其丢弃。 在执行一系列通道读取或放置操作之后,调用此方法以准备一系列通道写入或相对get操作。例如: