我正在将值写入文件。
值写入正确。在另一个应用程序中,我可以毫无例外地读取文件。
但是在我的新应用程序中,我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());
}
}
}