繁体   English   中英

尝试复制哈希图存储时出错

[英]Error when trying to copy a hashmap store

当我尝试复制商店时,出现一些错误。 错误是:

 choice:java.io.NotSerializableException: Employee
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at java.util.HashMap.writeObject(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at FileUtility.save(FileUtility.java:27)
    at MainApp.start(MainApp.java:190)
    at MainApp.main(MainApp.java:16)

这也是我的代码:MainApp

//---------------------------------------------------------------------------------------
//  Name:        Case 7: Store.
//  Description: Choice 7 gives the user an option to copy and read a store
//               using read and write class from Java.
//---------------------------------------------------------------------------------------
            case 7:
                System.out.println("Store");
                EmployeeStore copyMyStore = Store.copy();
                System.out.println(copyMyStore); //print contents to check equality
                //shallow copy would look like this...
                EmployeeStore shallowCopyMyStore;
                //both variables point to same object
                shallowCopyMyStore = Store; 
                //lets store and re-load the mystore object
                FileUtility.save("myStore.store", Store);
                /*EmployeeStore loadedStore 
                            = (EmployeeStore)FileUtility.load("myStore.nmcg");
                System.out.println("\n--- RELOADED STORE CONTENTS ---");
                loadedStore.print();
                //System.out.println("\n(static) Count: "  + EmployeeStore.print);*/


                break;

文件工具

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;


/**
  * <i>Use this class to save and load any type of object (i.e. a container class (e.g. PersonStore), an account, a task, a person)
 *   @author     NMCG
 *   @version    1.0            
*/
public class FileUtility /*serialization - step 2*/
{

    /**
     * @param fileName relative or absolute file path (e.g. "name.txt" or "c:\\temp\\name.txt"
     * @param obj      address of any object to be stored (i.e. a container class (e.g. PersonStore), an account, a task, a person)
     */

    public static void save(String fileName, Object obj)
    {
        try
        {
            FileOutputStream fos = new FileOutputStream(fileName);
            ObjectOutputStream oos = new ObjectOutputStream(fos);

            oos.writeObject(obj);
            oos.flush();
            oos.close();
            fos.close();
        }
        catch(Exception e)
        {
            System.out.println("Something bad happened during the save phase!");
            e.printStackTrace();
        }
    }

    /**
     * @param  fileName relative or absolute file path (e.g. "name.txt" or "c:\\temp\\name.txt")
     * @return Object   address of the object loaded into RAM from file
     */

    public static Object load(String fileName)  // "test.txt"
    {
        Object objectIn = null;
        try
        {
            FileInputStream fis = new FileInputStream(fileName);
            ObjectInputStream ois = new ObjectInputStream(fis);

            objectIn = ois.readObject();

            ois.close();
            fis.close();
        }
        catch(Exception e)
        {
            System.out.println("Something bad happened during the save phase!");
            e.printStackTrace();
        }
        return objectIn;
    }
}

复制

// ---------------------------------------------------------------------------------------
// Name: Store copy.
// ---------------------------------------------------------------------------------------
public EmployeeStore copy()
{
    // instanciate the destination copy and give same name
    EmployeeStore Copy = new EmployeeStore();

    // by specifying the type of the entry in the for loop i.e. <>
    // we don't have to typecast the getKey() and getValue() methods
    // as shown in the commented out line inside the for loop
    for (Map.Entry<String, Employee> entry : map.entrySet()) 
    {
        // getting each key-value and putting into the copy
        // theCopy.add((MovieKey)entry.getKey(), (Movie)entry.getValue());
        Copy.add(entry.getValue());
    }
    // return address of the new MovieStore object
    return Copy;
}
// ---------------------------------------------------------------------------------------

错误消息说明了一切: Employee类不可序列化。 它没有实现java.io.Serializable接口。

查找NotSerializableException 顾名思义,当期望对象实现Serializable接口但不实现时,将抛出该异常。 异常报告甚至告诉您实现接口所需的类,在您的情况下为Employee

选择:java.io.NotSerializableException:员工

您的Employee类应实现Serializable接口。 您发布的代码有些不相关且无用...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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