Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

184
Vistas
Load Lists from Serialized file

For a college project i have to save a hashmap of Lists to a binary file. Then i have to be able to load it again but im having a bit of trouble. It will save my file but will not load it. Here is my code:

This is Saving:

private static void storeRec() 
{


    try
    {
        File f = new File("recommendation.dat");
        if(!f.exists())
        {
            f.createNewFile();
        }   
        FileOutputStream fos=new FileOutputStream(f);
        ObjectOutputStream oos=new ObjectOutputStream(fos);

        oos.writeObject(localStore);
        oos.flush();
        oos.close();
        fos.close();
        System.out.println("Recommendation read to File");


    } 
    catch (IOException ex) 
    {
        Logger.getLogger(Project2.class.getName()).log(Level.SEVERE, null, ex);
    }



}

This is the loading code :

 List<Recommendation> newmovies = new ArrayList<>();

    try
    {
        File f = new File("recommendation.dat");
        if(f.exists())
        {
            DataInputStream dis = new DataInputStream(new FileInputStream(f));
            /*FileInputStream streamIn = new FileInputStream(f);
            ObjectInputStream dis = new ObjectInputStream(streamIn);*/
            while(dis.available()>0)
            {
                byte[] titleBytes = new byte[32];
                dis.read(titleBytes);
                String title = new String(titleBytes);
                byte[] queryBytes = new byte[32];
                dis.read(queryBytes);
                String query = new String(queryBytes);
                byte[] directorBytes = new byte[32];
                dis.read(directorBytes);
                String director = new String(directorBytes);
                byte[] summaryBytes = new byte[64];
                dis.read(summaryBytes);
                String summary = new String(summaryBytes);

                byte[] categoryBytes = new byte[18];
                dis.read(categoryBytes);
                String category = new String(categoryBytes);  
               //String category = dis.readUTF();
                double rating = dis.readDouble();
                int release = dis.readInt(); 
                byte[] castBytes = new byte[64];
                dis.read(castBytes);
                String cast= new String(castBytes);
                ArrayList<String> castArrayList = new ArrayList<>(Arrays.asList(cast.split(",")));
                int myRating = dis.readInt();
                byte[] commentsBytes = new byte[32];
                dis.read(commentsBytes);
                String myComments = new String(commentsBytes);

                newmovies.add(new Recommendation(title,query,director,summary,release,category,rating,castArrayList,myRating,myComments));
                //System.out.println(newmovies);
                localStore.put(query, newmovies);

            }
        }

LocalStore is a hashmap i would like to add the data from the file to. The key is the Query. For some reason it will not add to the map

Any help would be very much appreciated.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You have to use an ObjectInputStream on a file created by ObjectOutputStream, and readObject()to read an object written by writeObject().

And available() is not a valid test for end of stream. See the Javadoc. You have to catch EOFException.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda