Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

177
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda