我如何serialize Enumeration对象到一个文件,然后deserialize它?我试着把它转换成ArrayList,但是这个选项对我不起作用。我尝试了以下代码:
FileOutputStream fos = null;
ObjectOutputStream outs = null;
Enumeration<TreePath> stateEnum = com.jidesoft.tree.TreeUtils.saveExpansionStateByTreePath(tree);
ArrayList<TreePath> pathList = new ArrayList<TreePath>();
while(stateEnum.hasMoreElements()){
pathList.add(stateEnum.nextElement());
}
try {
fos = new FileOutputStream(TREE_STATE_FILE);
outs = new ObjectOutputStream(fos);
outs.writeObject(pathList);
outs.close();
} catch (IOException e) {
_log.info("Failed to create " + TREE_STATE_FILE + " file: ", e);
}但是当我尝试serialize它的时候,我得到的结果是空的。谢谢
发布于 2012-08-29 13:56:39
我不确定这是不是你的意思,但看看这段代码:
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.NoSuchElementException;
@SuppressWarnings("unchecked")
public class SerializableEnumeration
extends ArrayList
implements Enumeration
{
/** The serialVersionUID */
private static final long serialVersionUID = 8678951571196067510L;
private int index;
public SerializableEnumeration () {
index = 0;
}
public SerializableEnumeration (Collection c) {
super(c);
index = 0;
}
public SerializableEnumeration (int initialCapacity) {
super(initialCapacity);
index = 0;
}
public boolean hasMoreElements() {
return (index < size());
}
public Object nextElement() throws NoSuchElementException
{
try {
Object nextObj = get(index);
index++;
return nextObj;
}
catch (IndexOutOfBoundsException e) {
throw new NoSuchElementException();
}
}
private void writeObject(java.io.ObjectOutputStream out)
throws java.io.IOException
{
// the only thing to write is the index field
out.defaultWriteObject();
}
private void readObject(java.io.ObjectInputStream in)
throws java.io.IOException, ClassNotFoundException
{
in.defaultReadObject();
}
}来自here
发布于 2012-08-29 14:20:01
在国际海事组织,Enumeration的序列化并没有多大意义。Enumeration仅仅是一个位于数据结构或其他业务逻辑之上的浮动视图。为了序列化,你最好坚持序列化后端接口。为了可视化我正在编写的内容,我设置了一个小代码片段:
import java.io.*;
import java.util.*;
import javax.swing.tree.TreePath;
public class EnumTest {
public class MyEnumerator<T> {
// set holding data
List<T> data;
public MyEnumerator(List<T> data) {
this.data = data;
}
public List<T> getData() {
return data;
}
public Enumeration<T> enumerate() {
return new Enumeration<T>() {
transient int i = 0;
@Override
public boolean hasMoreElements() {
return i < data.size();
}
@Override
public T nextElement() {
return data.get(i++);
}
};
}
}
public EnumTest() throws Exception {
List<TreePath> TreePaths = Arrays.asList(new TreePath[] { new TreePath("3"), new TreePath("4"), new TreePath("5") });
MyEnumerator<TreePath> myEnum1 = new MyEnumerator<TreePath>(TreePaths);
print(myEnum1);
FileOutputStream fos = new FileOutputStream("test.out");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(myEnum1.getData());
oos.close();
fos.close();
System.out.println("* Serialization complete");
FileInputStream fis = new FileInputStream("test.out");
ObjectInputStream ois = new ObjectInputStream(fis);
@SuppressWarnings("unchecked")
List<TreePath> data = (List<TreePath>) ois.readObject();
MyEnumerator<TreePath> myEnum2 = new MyEnumerator<TreePath>(data);
print(myEnum2);
System.out.println("* Deserialization complete");
}
private void print(MyEnumerator<TreePath> myEnum1) {
Enumeration<TreePath> enm = myEnum1.enumerate();
while (enm.hasMoreElements()) {
System.out.println(enm.nextElement());
}
}
public static void main(String[] args) throws Exception {
new EnumTest();
}
}这绕过了序列化Enumeration本身的主题,方法是持久化所包含的数据,然后重新构造包装器类MyEnumerator。通过序列化/反序列化循环,您将获得一个不同的对象,但该对象在语义上是相同的。
发布于 2012-08-29 13:56:19
为了让您的代码工作,TreePath还必须是可序列化的,因为pathList的元素是TreePath对象。
https://stackoverflow.com/questions/12171614
复制相似问题