datainputstream用于文件最后一行的EOFException

时间:2013-12-04 11:54:06

标签: java datainputstream eofexception

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

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

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);

        }
    }

有人可以帮我这个吗? ty先进。

1 个答案:

答案 0 :(得分:1)

你写了一个新的行字符,但没有读它