gpt4 book ai didi

java - Java的BufferunderflowException

转载 作者:行者123 更新时间:2023-12-01 12:40:37 25 4
gpt4 key购买 nike

我正在将值写入文件。

值写正确。在另一个应用程序中,我可以毫无异常(exception)地读取文件。

但是在我的新应用程序中,尝试读取文件时收到了Bufferunderflowexception

我花了几天的时间来解决这个问题,但我只是不知道如何解决。

也做了很多研究。
bufferunderflowexception指的是:

Double X1 = mappedByteBufferOut.getDouble(); //8 byte (double)

这是我读取文件的代码:
 @Override
public void paintComponent(Graphics g) {

RandomAccessFile randomAccessFile = null;
MappedByteBuffer mappedByteBufferOut = null;
FileChannel fileChannel = null;

try {
super.paintComponent(g);

File file = new File("/home/user/Desktop/File");

randomAccessFile = new RandomAccessFile(file, "r");

fileChannel = randomAccessFile.getChannel();

mappedByteBufferOut = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, randomAccessFile.length());

while (mappedByteBufferOut.hasRemaining()) {

Double X1 = mappedByteBufferOut.getDouble(); //8 byte (double)
Double Y1 = mappedByteBufferOut.getDouble();
Double X2 = mappedByteBufferOut.getDouble();
Double Y2 = mappedByteBufferOut.getDouble();
int colorRGB = mappedByteBufferOut.getInt(); //4 byte (int)
Color c = new Color(colorRGB);

Edge edge = new Edge(X1, Y1, X2, Y2, c);

listEdges.add(edge);

}
repaint();

for (Edge ed : listEdges) {
g.setColor(ed.color);
ed = KochFrame.edgeAfterZoomAndDrag(ed);
g.drawLine((int) ed.X1, (int) ed.Y1, (int) ed.X2, (int) ed.Y2);
}
}
catch (IOException ex)
{
System.out.println(ex.getMessage());
}
finally
{
try
{
mappedByteBufferOut.force();
fileChannel.close();
randomAccessFile.close();
listEdges.clear();
} catch (IOException ex)
{
System.out.println(ex.getMessage());
}
}
}

我希望有一个人可以帮助我。

最佳答案

从java.nio.ByteBuffer的docs中:

Throws: BufferUnderflowException - If there are fewer than eight bytes remaining in this buffer



我认为这很清楚这是异常的来源。为了修复它,您需要确保ByteBuffer中有足够的数据以便读取double(8个字节),而不是hasRemaining(),后者仅检查一个字节:
while (mappedByteBufferOut.remaining() >= 36) {//36 = 4 * 8(double) + 1 * 4(int)

关于java - Java的BufferunderflowException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20644505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com