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

标签 java datainputstream eofexception

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

级别 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/

相关文章:

java - 在 for 语句中使用 if 语句

java - 每次在JAVA中调用函数时在Excel中添加行

java - 从 DataInputStream 中读取一个 int[]

java - 数据输入流和 UTF-8

java.io.EOFException while writing and read from a servlet

tomcat - java.io.EOFException : Unexpected EOF read on the socket 异常

java - 如何将 Pdf 转换为 base64 并编码/解码

java - 递归需要不必要的返回并重新启动应用程序

java - PushBackInputStream和DataInputStream,如何推回一个double?

Java:防止 Socket DataInputStream 抛出 EOFException