1

I'm trying to deserialize a file to display what I stored instead of the byte it stores it as - any help?

public Object getObject(String obj) throws IOException, ClassNotFoundException{
        // Create the stream objects.
        String file = "files\\" + obj + ".dat";
        FileInputStream inStream = new FileInputStream(file);
        ObjectInputStream objectInputFile = new ObjectInputStream(inStream);

        ArrayList<Object> records = new ArrayList<Object>();

        // Read the serialized objects from the file.
        try{
            records.add(objectInputFile.readObject().toString());
        }catch(EOFException e){
            System.out.println("ERROR " + e);
        }

        // Close the file.
        objectInputFile.close();

        // Display the objects.
        System.out.println(records);


        return records;     
    }

When run, it outputs

[[[[[Staff@41e1d4bd], Staff@34741df1]], Staff@441f8094]]

How do I get this byte to text?

EDIT:

After a few changes my code below is now erroring:

public Staff getObject(String obj) throws IOException, ClassNotFoundException{
        // Create the stream objects.
        String file = "files\\" + obj + ".dat";
        FileInputStream inStream = new FileInputStream(file);
        ObjectInputStream objectInputFile = new ObjectInputStream(inStream);

        Staff records = null;

        // Read the serialized objects from the file.
        try{
            records = (Staff)objectInputFile.readObject();
        }catch(EOFException e){
            System.out.println("ERROR " + e);
        }

        // Close the file.
        objectInputFile.close();

        // Display the objects.
        System.out.println(records);


        return records;     
    }

The new error:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to Staff
theglobin
  • 33
  • 6

0 Answers0