datainputstream for EOFException on the last line of the file

102 views Asked by At

I try to write a data file, whitch will contain:

level x1 y1 x2 y2 rgbcolorcode x1 y1 x2 y2 rgbcolorcode x1... etc

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

but with the code below i get an EOFException on the very last line.

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

        }
    }

could someone help me with this? ty in advanced.

1

There are 1 answers

0
Siarhei On BEST ANSWER

You write a new line character but not reading it