以下程序运行时出现如下错误,求解
错误信息如下:
java.io.NotSerializableException: ch04.LogModel
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)
at java.util.ArrayList.writeObject(ArrayList.java:570)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:945)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1461)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)
at ch04.LogFileOperate.writeLogFile(LogFileOperate.java:54)
at ch04.Cilent.main(Cilent.java:25)
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: ch04.LogModel
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1333)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at java.util.ArrayList.readObject(ArrayList.java:593)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:974)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1849)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at ch04.LogFileOperate.readLogFile(LogFileOperate.java:32)
at ch04.Cilent.main(Cilent.java:27)
Caused by: java.io.NotSerializableException: ch04.LogModel
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)
at java.util.ArrayList.writeObject(ArrayList.java:570)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:945)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1461)readlog====null
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)
at ch04.LogFileOperate.writeLogFile(LogFileOperate.java:54)
at ch04.Cilent.main(Cilent.java:25)
源代码如下:
package ch04;
public class LogModel {
private String logId;
private String operateUser;
private String operateTime;
private String logContent;
public String getLogId() {
return logId;
}
public void setLogId(String logId) {
this.logId = logId;
}
public String getOperateUser() {
return operateUser;
}
public void setOperateUser(String operateUser) {
this.operateUser = operateUser;
}
public String getOperateTime() {
return operateTime;
}
public void setOperateTime(String operateTime) {
this.operateTime = operateTime;
}
public String getLogContent() {
return logContent;
}
public void setLogContent(String logContent) {
this.logContent = logContent;
}
@Override
public String toString() {
return "logID="+logId+",operateUser="+operateUser+",operateTime="+operateTime+",logContent="+logContent;
}
}
package ch04;
import java.util.List;
public interface LogFileOperateApi {
public List<LogModel> readLogFile();
public void writeLogFile(List<LogModel> list);
}
package ch04;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;
public class LogFileOperate implements LogFileOperateApi {
private String logFilePathName = "AdapterLog.log";
public LogFileOperate(String logFilePathName){
if(logFilePathName!=null&&logFilePathName.trim().length()>0){
this.logFilePathName = logFilePathName;
}
}
@Override
public List<LogModel> readLogFile() {
// TODO Auto-generated method stub
List<LogModel> list = null;
ObjectInputStream oin = null;
try{
File f = new File(logFilePathName);
if(f.exists()){
oin = new ObjectInputStream(new BufferedInputStream(new FileInputStream(f)));
}
list = (List<LogModel>) oin.readObject();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(oin!=null){
oin.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
return list;
}
@Override
public void writeLogFile(List<LogModel> list) {
// TODO Auto-generated method stub
File f = new File(logFilePathName);
ObjectOutputStream oout = null;
try{
oout= new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(f)));
oout.writeObject(list);
}catch(IOException e){
e.printStackTrace();
}finally{
try{
oout.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
package ch04;
import java.util.ArrayList;
import java.util.List;
public class Cilent {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
LogModel lml = new LogModel();
lml.setLogId("0000000000001");
lml.setOperateUser("admin");
lml.setOperateTime("2011-7-9 11:47:25");
lml.setLogContent("这仅仅是一个测试");
List<LogModel> list = new ArrayList<LogModel>();
list.add(lml);
LogFileOperateApi api = new LogFileOperate("F:\\WorkSpaces\\shejimoshi\\AdapterLog.log");
api.writeLogFile(list);
List<LogModel> readLog = api.readLogFile();
System.out.println("readlog===="+readLog);
}
}