Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!