1个entity:class Person implements Serializable{ private static final long serialVersionUID = 4212275679707232020L;
private String name,address; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
}
}
question: public static void main(String[] args) throws Exception {
serializableObject();
//如果这里是通过socket的OutputStream输出到1个ServerSocket,那么该如何反序列化呢?
//Server端并没有我这个entity的类型,是通过反射动态构造出这样1个类型吗,如果是,怎么做呢?
}
public static void serializableObject() throws Exception{
File file = new File("F:"+File.separator+"person.ini");
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
Person per = new Person();
per.setAddress("深圳");
per.setName("Sean.zhang");
oos.writeObject(per);
oos.close();
}
private String name,address; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
}
}
question: public static void main(String[] args) throws Exception {
serializableObject();
//如果这里是通过socket的OutputStream输出到1个ServerSocket,那么该如何反序列化呢?
//Server端并没有我这个entity的类型,是通过反射动态构造出这样1个类型吗,如果是,怎么做呢?
}
public static void serializableObject() throws Exception{
File file = new File("F:"+File.separator+"person.ini");
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
Person per = new Person();
per.setAddress("深圳");
per.setName("Sean.zhang");
oos.writeObject(per);
oos.close();
}
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Data
implements Serializable
{ private int i; Data(int x)
{
i = x;
} public String toString()
{
return Integer.toString(i);
}
}
public class Worm
implements Serializable
{ // Generate a random int value:
private static int r()
{
return (int) (Math.random() * 10);
} private Data[] d = {new Data(r()), new Data(r()), new Data(r())}; private Worm next; private char c; private String str = "A worm"; // Value of i == number of segments
Worm(int i, char x)
{
System.out.println(" Worm constructor: " + i);
c = x;
if (--i > 0)
next = new Worm(i, (char) (x + 1));
} Worm()
{
System.out.println("Default constructor");
} public String toString()
{
String s = ":" + c + "(";
for (int i = 0; i < d.length; i++)
s += d[i].toString();
s += ")";
if (next != null)
s += next.toString();
return s;
} public static void main(String[] args)
{
Worm w = new Worm(6, 'a');
System.out.println("w = " + w);
try
{
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("worm.out"));
out.writeObject("Worm storage");
out.writeObject(w);
out.writeObject(9527);
out.close(); // Also flushes output
ObjectInputStream in = new ObjectInputStream(new FileInputStream("worm.out"));
String s = (String) in.readObject();
Worm w2 = (Worm) in.readObject();
int myNum = (Integer) in.readObject();
System.out.println(s + ", w2 = " + w2 + ";myNum = " + myNum);
}
catch (Exception e)
{
e.printStackTrace();
}
try
{
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bout);
out.writeObject("Worm storage");
out.writeObject(w);
out.writeObject(1234);
out.flush();
ObjectInputStream in =
new ObjectInputStream(new ByteArrayInputStream(bout.toByteArray()));
String s = (String) in.readObject();
Worm w3 = (Worm) in.readObject();
int myNum = (Integer) in.readObject();
System.out.println(s + ", w3 = " + w3 + ";myNum = " + myNum);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}Main方法里的序列化和反序列化操作,少了一个传输操作。
把Person的源文件发到服务端,然后JavaCompiler编译了一下,然后反序列化传递的对象,但是最恶心的地方是package的名字服务端也要保持一样。。