简体   繁体   中英

How to serialize/deserialize two different list types into single sequential file

I have three different lists of different contact types that I need to serialize into a single file and then retrieve those lists when needed (deserialize). I've thought about using a hashmap but I'm not familiar with and I'm sure how I would retrieve the lists intact. Any ideas are appreciated.

I'm not sure if I can just use an Object type in the hashmap. It's the one way I'm able to add all three lists to the hashmap. Also I need to know the correct way to retrieve those lists from the hashmap if that's the best approach.

public class Controller()
{
    // the list objects I need to serialize
    List<FamilyContact> friendContacts = new ArrayList<FamilyContact>();
    List<Contact> fdContacts = new ArrayList<>(friendContacts);

    List<FamilyContact> familyContacts = new ArrayList<FamilyContact>();
    List<Contact> fContacts = new ArrayList<>(familyContacts);

    // methods to retrieve the lists and list items
}


//Serialization code
public class Serialization
{


    public void serialize(HashMap<String, Object> lists, String fileName)
    {

        // serializes the hashmap passed from calling method
        try (ObjectOutputStream output = 
                new ObjectOutputStream(new FileOutputStream(fileName)))
        {
            output.writeObject(lists);
            output.close();
        }
        catch(IOException ex)
        {
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }
    }

    @SuppressWarnings("unchecked")
    public HashMap<String, Object> deserialize(String fileName)
    {
        try (ObjectInputStream input = 
                new ObjectInputStream(new FileInputStream(fileName)))
        {
            HashMap<String, Object> lists = (HashMap)input.readObject();
            System.out.println(lists.size());
            input.close();
            return lists;
        }
        catch (IOException | ClassNotFoundException ex)
        {
            if(ex.getClass().getName() == "java.io.FileNotFoundException")
            {
                showErrorDialog("File Not Found", "Contacts.ser not found");
            }
            else
            {
                System.out.println(ex.getClass().getName());
                System.out.println(ex.getMessage());
            }
        }
        return null;
    }

When you deserialise an object it [usually] comes out as the same type as you serialised it. So serialisation is a red herring.

Using a Map in such situations is usually an unnecessary thing to do. Particularly as you have different types, which will lead to casting.

So you will probably want an [immutable] value type, written in the usual [overly verbose] way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM