【问题标题】:ObjectInputStream isn't finding any data to pull?ObjectInputStream 没有找到任何要提取的数据?
【发布时间】:2022-01-13 19:56:46
【问题描述】:

我正在尝试为绘图程序读取形状对象并将其写入文件,但是当我尝试从文件中读取时,它显示文件为空。该文件肯定正在被写入和更新,但是当尝试从文件中读取时,它显示有零字节可用。形状类是可序列化的,所以我不确定为什么这根本不起作用。

public void writeToFile() {
        try {
            FileOutputStream fileOut = new FileOutputStream("C:\\Users\\johnm\\eclipse-workspace\\CSE205_Assignment05\\save.ser");
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            for (Shape item : shapes) {
                out.writeObject(item);
            }
            out.close();
            fileOut.close();
            System.out.println("Serialized data is saved in output.ser");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public void loadFromFile() {
        boolean cont = true;
        Shape shape = null;
        int count = 0;
        while (cont) {
            try {
                FileInputStream fileIn = new FileInputStream("C:\\Users\\johnm\\eclipse-workspace\\CSE205_Assignment05\\save.ser");
                ObjectInputStream in = new ObjectInputStream(fileIn);
                System.out.println(in.available() + " Bytes");
                if (in.available() != 0) {
                    shape = (Shape) in.readObject();
                    if (shape != null) {
                        shapes.add(shape);
                        count++;
                    } else {
                        System.out.println("Shape is null");
                    }
                } else {
                    cont = false;
                }
                in.close();
                fileIn.close();
                System.out.println("Deserialized " + count + " Objects");
            } catch (ClassNotFoundException c) {
                System.out.println("Class not found");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

【问题讨论】:

    标签: java inputstream objectinputstream


    【解决方案1】:

    好吧,由于某种原因,.available() 方法不显示任何字节,无论是否实际存在任何字节。为了解决这个问题,我只添加了另一个 try/catch 语句,它会持续读取对象,直到遇到 EOFException 并捕获自身。

    我的代码在运行后最终看起来像这样。

    public void loadFromFile() {
            //** Loads set of shape objects from file
            Shape shape = null;
            int count = 0;
            try {
                FileInputStream fileIn = new FileInputStream("save.ser");
                ObjectInputStream in = new ObjectInputStream(fileIn);
                System.out.println(in.available() + " Bytes");
                try {
                    while (true) {
                        shape = (Shape) in.readObject();
                        if (shape != null) {
                            shapes.add(shape);
                            count++;
                        } else {
                            System.out.println("Shape is null");
                        }
                    }
                } catch (EOFException e) {
                    System.out.println("End of file exception");
                }
                in.close();
                fileIn.close();
                System.out.println("Deserialized " + count + " Objects");
                repaint();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-16
      • 2022-01-21
      • 2021-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多