当我尝试复制商店时,我遇到了一些错误。错误是:
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;
}
// ---------------------------------------------------------------------------------------