ERROR: java.lang.ClassCastException: frsf.isi.died.tp.modelo.productos.Video can not be cast to [Lfrsf.isi.died.tp.modelo.productos.Video;

0

I have a casting problem (apparently). This is the error that returns me eclipse: java.lang.ClassCastException: frsf.isi.died.tp.modelo.productos.Video can not be cast to [Lfrsf.isi.died.tp.modelo.productos.Video;

I am trying to recover my "Video" class objects that are saved in a file. When I try to create an Array of "Video []" to be able to recover the elements of the file I get that error. I leave the part of the code where the error is located:

    public static void guardarVideo(Video unVideo) {
    Video video = unVideo;


    try {

        ObjectOutputStream  escribiendo = new ObjectOutputStream(new FileOutputStream("F:/JAVA Projectos/taller01/Datos/Videos.txt",true));

        escribiendo.writeObject(video);

        escribiendo.close();

        ObjectInputStream leyendo = new ObjectInputStream(new FileInputStream("F:/JAVA Projectos/taller01/Datos/Videos.txt"));
        //Este es el error 
        Object[] recuperado = (Video[]) leyendo.readObject();

        leyendo.close();

        for(Object v : recuperado) {
            System.out.println(v);

        }

            }catch(Exception e) {
                e.printStackTrace();
            }

        }
    
asked by Rodro 30.07.2018 в 00:02
source

1 answer

0

I think the casting method is not the correct one, in itself the method returns an object but it does not mean that you have to reference the Object class. You have to refer to the same class you're casting.

Video[] video = (Video[])leyendo.readObject();
    
answered by 30.07.2018 в 16:34