ObjectInputStream 没有找到任何要提取的数据?

Posted

技术标签:

【中文标题】ObjectInputStream 没有找到任何要提取的数据?【英文标题】:ObjectInputStream isn't finding any data to pull? 【发布时间】: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();
            
        
    

【问题讨论】:

【参考方案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();
        
    

【讨论】:

以上是关于ObjectInputStream 没有找到任何要提取的数据?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Java 中检查 ObjectInputStream 是不是为空?

Java--ObjectInputStream和ObjectOutputStream

IO_ObjectInputStream&ObjectOutputStream

IO流入门-第十二章-ObjectInputStream_ObjectOutputStream

ObjectInputStream没有读入

ObjectInputStream - 读取对象 - 有没有办法阻止调用无参数超类构造函数?