我将图表对象写入文件。图表类是......
import java.io.Serializable;
import java.util.ArrayList;
public class Graph implements Serializable{
public String gid = "";
public ArrayList<Vertex> vertices = new ArrayList<Vertex>();
public ArrayList<Edge> edges = new ArrayList<Edge>();
public Graph()
{
}
public Graph(String id, ArrayList<Vertex> ver, ArrayList<Edge> ed)
{
gid = id;
vertices = ver;
edges = ed;
}
public String getId()
{
return gid;
}
public void Clear()
{
vertices.clear();
edges.clear();
}
}
Vertex 和 Edge 类如下...
import java.io.Serializable;
public class Vertex implements Serializable{
public int vertexId;
public String vertexLabel;
public Vertex()
{
vertexId = 0;
vertexLabel = "";
}
public Vertex(int id, String label)
{
vertexId = id;
vertexLabel = label;
}
public String getVertexLabel()
{
return vertexLabel;
}
public String toString()
{
return vertexId + "" + vertexLabel;
}
}
import java.io.Serializable;
public class Edge implements Serializable{
public int sid;
public int tid;
public Vertex src;
public Vertex tar;
public String bondtype;
int bt;
public Edge()
{
sid = 0;
tid = 0;
src = new Vertex();
tar = new Vertex();
bondtype = "";
bt = 0;
}
public Edge(int sourceId,int targetId,Vertex source,Vertex target,int bt)
{
sid = sourceId;
tid = targetId;
src = source;
tar = target;
this.bt = bt;
if(bt == 1)
bondtype = "s";
else if(bt == 2)
bondtype = "d";
else
bondtype = "t";
}
public String toString()
{
return src.vertexLabel + "," + bondtype + "," + tar.vertexLabel;
}
public String reverseString()
{
return tar.vertexLabel + "," + bondtype + "," + src.vertexLabel;
}
public String toNumString()
{
return src.vertexId + "," + bondtype + "," + tar.vertexId;
}
public int getSourceId(Edge edge)
{
int source = 0;
source = edge.sid;
return source;
}
public int getTargetId(Edge edge)
{
int target = 0;
target = edge.tid;
return target;
}
}
我编写和阅读图形对象到/从文件
public static ArrayList<Graph> ReadGraphs(String filename)
{
ArrayList<Graph> graphs = new ArrayList<Graph>();
ObjectInputStream ois = null;
Graph obj = new Graph();
try{
ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(filename), 16 * 1024));
while( (obj = (Graph) ois.readObject()) != null)
{
if (obj instanceof Graph)
{
graphs.add(obj);
}
}
}catch (EOFException ex) { //This exception will be caught when EOF is reached
//System.out.println("End of file reached.");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
if (ois != null) {
ois.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return graphs;
}
public static void writeG(Graph g) throws IOException
{
File myFile = new File("./Graphs.txt");
int bufferSize = 16 * 1024;
if(!myFile.exists())
{
myFile.createNewFile();
FileOutputStream fos = new FileOutputStream(myFile);
ObjectOutputStream os = new ObjectOutputStream(new BufferedOutputStream(fos,bufferSize));
try
{
os.writeObject(g);
}
catch (IOException e)
{
e.printStackTrace();
}
os.flush();
os.close();
}
else
{
FileOutputStream fos = new FileOutputStream(myFile,true);
NoHeaderObjectOutputStream oos = new NoHeaderObjectOutputStream(new BufferedOutputStream(fos, bufferSize));
try
{
oos.writeObject(g);
}
catch (IOException e)
{
e.printStackTrace();
}
oos.flush();
oos.close();
}
}
当我运行它时,我得到以下类强制转换异常。以下是我遇到的问题的堆栈跟踪:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: cannot assign instance of java.lang.String to field InputGraphPhase.Edge.src of type InputGraphPhase.Vertex in instance of InputGraphPhase.Edge
at java.io.ObjectStreamClass$FieldReflector.setObjFieldValues(ObjectStreamClass.java:2083)
at java.io.ObjectStreamClass.setObjFieldValues(ObjectStreamClass.java:1261)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1996)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1915)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1798)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
at java.util.ArrayList.readObject(ArrayList.java:771)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1017)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1893)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1798)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1990)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1915)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1798)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
at InputGraphPhase.GraphParser.ReadGraphs(GraphParser.java:501)
at GUI.MyFrame.check(MyFrame.java:1135)
at GUI.MyFrame.showResult(MyFrame.java:1328)
at GUI.MyFrame$6.actionPerformed(MyFrame.java:671)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
答案 0 :(得分:2)
cannot assign instance of java.lang.String to field InputGraphPhase.Edge.src of type InputGraphPhase.Vertex in instance of InputGraphPhase.Edge
自序列化以来,您已更改Edge
类。序列化后,src
为String
;当你反序列化它时,它被声明为Vertex
。
此循环无效:
while( (obj = (Graph) ois.readObject()) != null)
readObject()
在流的末尾不会返回null
。它抛出EOFException
。只要序列化一个null
,就可以在流中的任何位置出现{{1}}。
序列化文件不是文本文件。它是二进制的。