import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import com.thoughtworks.xstream.XStream;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
HashMap hm = new HashMap();
hm.put("string1", new ForSer("string1", 10));
hm.put("string2", new ForSer("string2", 101));
hm.put("string3", new ForSer("string3", 1011));
hm.put("string4", new ForSer("string4", 10111));
// FileOutputStream fos = new FileOutputStream("d:/foo.xml");
// XMLEncoder xenc = new XMLEncoder(fos);
// xenc.writeObject(hm);
// xenc.flush();
// xenc.close();
XStream xstream = new XStream();
System.out.println(xstream.toXML(hm));
//Binary:
ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos) ;
oos.writeObject(new ForSer("string", 111));
byte[] bar = baos.toByteArray();
System.out.println(new String(bar));
}
static class ForSer implements Serializable {
private static final long serialVersionUID = 1L;
private String str;
private int count;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public ForSer(String str, int count) {
super();
this.str = str;
this.count = count;
}
}
}
5/26/08
Java Binary / XML Serialization
XMLEncoder under Java 5 did not cope with serialization task:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment