我尝试写一个数据文件,其中包含:
等级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/