gpt4 book ai didi

java - 文件最后一行的 EOFException 的 datainputstream

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

我尝试编写一个数据文件,其中将包含:

级别 x1 y1 x2 y2 rgbcolorcode x1 y1 x2 y2 rgbcolorcode x1...等

level - int
x1-y2 are doubles
rgbcolorcode is an int.

但是通过下面的代码,我在最后一行得到了 EOFException。

private void changeLevelDataWOutBuffer() {
edges = new ArrayList<>();
try {
DataInputStream fileDataInputStream = new DataInputStream(new FileInputStream(filepath + "dwob.txt"));
int i=0;
Double x1;
Double x2;
Double y1;
Double y2;
x1 = 0.0;
x2=0.0;
y1=0.0;
y2=0.0;
int rgb=0;

try {
level = fileDataInputStream.readInt();
lblLvl.setText("Level: " + level);
} catch (NumberFormatException e) {
}
while (fileDataInputStream.read() !=-1) {
if(i==0){
x1 = fileDataInputStream.readDouble();
System.out.println("i:"+i+" x1:"+x1.toString());
i++;
}
if(i==1){
y1 = fileDataInputStream.readDouble();
System.out.println("i:"+i+" y1:"+y1.toString());
i++;
}
if(i==2){
x2 = fileDataInputStream.readDouble();
System.out.println("i:"+i+" x2:"+x2.toString());
i++;
}
if(i==3){
y2 = fileDataInputStream.readDouble();
System.out.println("i:"+i+" x1:"+y2.toString());
i++;
}
if(i==4){
rgb = fileDataInputStream.readInt(); //<--- get the error on this line, on the very last loop.
System.out.println("i:"+i+" rgb:"+rgb);
i=0;
}
edge = new Edge(x1, y1, x2, y2, new Color(rgb));
edges.add(edge);
}
fileDataInputStream.close();

} catch (IOException ex) {
Logger.getLogger(KochPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}




public static void createFileDataStreamWOBuffer(int level) {
try {
file = new File(filepath + "dwob.txt");
edges.clear();
koch.setLevel(level);
koch.generateBottomEdge();
koch.generateLeftEdge();
koch.generateRightEdge();
opsWOBuffer = new FileOutputStream(file, false);
dos = new DataOutputStream(opsWOBuffer);
dos.writeInt(level);
dos.writeBytes("" + System.getProperty("line.separator"));
try {
for (Edge e : edges) {
dos.writeDouble(e.X1);
dos.writeDouble(e.Y1);
dos.writeDouble(e.X2);
dos.writeDouble(e.Y2);
dos.writeInt(e.color.getRGB());
}
} catch (Exception e) {
} finally {
dos.close();
}
} catch (IOException ex) {
Logger.getLogger(opdracht2.class.getName()).log(Level.SEVERE, null, ex);

}
}

有人可以帮我解决这个问题吗?提前。

最佳答案

您写了一个新行字符但没有读取它

关于java - 文件最后一行的 EOFException 的 datainputstream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20374689/

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