0

I have 3 objects of type Fraction in a .dat file and I'm trying to read the contents of the .dat file using Java.

With the following block of code below, I keep getting a:

Exception in thread "main" java.lang.ClassCastException: class Fraction cannot be cast to class [LFraction; (Fraction and [LFraction; are in unnamed module of loader 'app')
    at Mod8Problem1.main(Mod8Problem1.java:26)

I've tried a few things but I keep getting the same exception. What would be the best way to fix this?

try(
        ObjectInputStream input = new ObjectInputStream(new FileInputStream("SerialF.dat"))
) {
    Fraction[] updatedFractions = (Fraction[]) (input.readObject());

    for (int i = 0; i < updatedFractions.length; i++) {
        System.out.println(updatedFractions[i]);
    }
}      
4
  • You are trying to deserialize a Fraction, but the variable you use to reference it is a Fraction[]. You'll need to loop an read each Fraction object individually; readObject() is returning a Fraction, not a Fraction[]. Commented Jul 10, 2020 at 23:04
  • @Dioxin can you help me to understand why this block of code works: try (ObjectInputStream input = new ObjectInputStream(new FileInputStream("array.dat")); ) int[] numbers = (int[]) (input.readObject()); Commented Jul 10, 2020 at 23:13
  • Because array.dat was serialized as an array. Arrays are objects, and can be serialized. However, SerialF is not a serialized array. Writing multiple objects to the same file is not the same as serializing an array. Sounds like SerialF serialized multiple objects to the same file, while array simply serialized an array (one object). Commented Jul 10, 2020 at 23:18
  • You wrote in your question: I have 3 objects of type Fraction in a .dat No, you don't. You can only serialize a single object in java, however that single object may be a Collection or an array, in other words a single object that contains several, other objects. How was file SerialF.dat created? Commented Jul 11, 2020 at 12:22

1 Answer 1

0

Your problem is here:

Fraction[] updatedFractions = (Fraction[]) (input.readObject());

input.readObject() returns Fraction, but you are trying to cast it to Fraction[], which is not a compatible type. You could read multiple Fractions into an array to achieve the same result, though. You could use an ArrayList to do so, like this:

ArrayList<Fraction> fractions = new ArrayList<>();

try (
    ObjectInputStream input = new ObjectInputStream(new FileInputStream("SerialF.dat"))
) {
    while (input.available() > 0) {
        fractions.add((Fraction) input.readObject);
        
    }
}

Fraction[] updatedFractions = fractions.toArray(new Fraction[0]);

for (Fraction fraction : updatedFractions) {
    System.out.println(fraction);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.