Java 尝试复制hashmap存储时出错

Java 尝试复制hashmap存储时出错,java,copy,hashmap,Java,Copy,Hashmap,当我试图复制一个商店时,我会遇到一些错误。错误是: 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 Sou

当我试图复制一个商店时,我会遇到一些错误。错误是:

 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)
这也是我的代码: 主应用程序

文件实用程序

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;
    }
}
import java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.ObjectInputStream;
导入java.io.ObjectOutputStream;
/**
*使用此类保存和加载任何类型的对象(即容器类(例如PersonStore)、帐户、任务、人员)
*@作者NMCG
*@version 1.0
*/
公共类FileUtility/*序列化-步骤2*/
{
/**
*@param fileName相对或绝对文件路径(例如“name.txt”或“c:\\temp\\name.txt”
*@param obj要存储的任何对象的地址(即容器类(例如PersonStore)、帐户、任务、人员)
*/
公共静态无效保存(字符串文件名,对象obj)
{
尝试
{
FileOutputStream fos=新的FileOutputStream(文件名);
ObjectOutputStream oos=新的ObjectOutputStream(fos);
oos.writeObject(obj);
oos.flush();
oos.close();
fos.close();
}
捕获(例外e)
{
System.out.println(“在保存阶段发生了一些不好的事情!”);
e、 printStackTrace();
}
}
/**
*@param fileName相对或绝对文件路径(例如“name.txt”或“c:\\temp\\name.txt”)
*@返回从文件加载到RAM中的对象的对象地址
*/
公共静态对象加载(字符串文件名)/“test.txt”
{
objectobjectin=null;
尝试
{
FileInputStream fis=新的FileInputStream(文件名);
ObjectInputStream ois=新ObjectInputStream(fis);
objectIn=ois.readObject();
ois.close();
fis.close();
}
捕获(例外e)
{
System.out.println(“在保存阶段发生了一些不好的事情!”);
e、 printStackTrace();
}
返回目标;
}
}
抄袭

//---------------------------------------------------------------------------------------
//名称:商店副本。
// ---------------------------------------------------------------------------------------
公共雇员商店副本()
{
//实例化目标副本并提供相同的名称
EmployeeStore Copy=新建EmployeeStore();
//通过指定for循环中的条目类型,即。
//我们不必对getKey()和getValue()方法进行类型转换
//如for循环中注释掉的行所示
对于(Map.Entry:Map.entrySet())
{
//获取每个键值并放入副本中
//copy.add((MovieKey)entry.getKey(),(Movie)entry.getValue());
Copy.add(entry.getValue());
}
//新MovieStore对象的返回地址
返回副本;
}
// ---------------------------------------------------------------------------------------

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

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

选项:java.io.NotS例外情况:雇员


您的Employee类应该实现可序列化接口。您发布的代码有些不相关和无用…

选项:java.io.NotSerializableException:Employee


你的Employee类应该实现Serializable接口。你发布的代码有些不相关和无用…

查找
NotSerializableException
。顾名思义,当一个对象需要实现
Serializable
接口但没有实现时,就会抛出它。异常报告甚至会告诉你是哪个c在你的例子中,它是需要实现接口的类,
Employee

查找
NotSerializableException
。顾名思义,当一个对象需要实现
Serializable
接口但没有实现时,会抛出它。异常报告甚至会告诉你它是哪个类需要实现t界面,
Employee
在您的案例中。

您的问题可能重复?您想知道为什么不让Employee实现可序列化?您的问题可能重复?您想知道为什么不让Employee实现可序列化?所以基本上我要做的就是扩展可序列化?实现nt Serializable。这是一个接口。阅读javadoc会告诉你所有这些。我为需要的两个类添加了导入,它工作了。谢谢。所以基本上我要做的就是扩展Serializable?实现Serializable。这是一个接口。阅读javadoc会告诉你所有这些。我为需要的两个类添加了导入,它工作了谢谢你。
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;
}
// ---------------------------------------------------------------------------------------