在写一个网络纸牌游戏需要序列化对象后再网络传输,但是看了类,不是很清楚。谁能给一个例子??
小弟在此先谢过了!

解决方案 »

  1.   

    昨天回了一个贴在就是序列化对象的http://community.csdn.net/Expert/topic/4907/4907540.xml?temp=.5387384
      

  2.   

    package csdn.basic;import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;class Student implements Serializable {
    String name; String sex; String date; String grade; String college; String cLass; String classDuty; String sextion; String sextionDuty; String address; String telephone; public Student(String name, String sex, String date, String grade,
    String college, String cLass, String classDuty, String sextion,
    String sextionDuty, String address, String telephone) {
    this.name = name;
    this.sex = sex;
    this.date = date;
    this.grade = grade;
    this.college = college;
    this.cLass = cLass;
    this.classDuty = classDuty;
    this.sextion = sextion;
    this.sextionDuty = sextionDuty;
    this.address = address;
    this.telephone = telephone;
    }
    }class FileOperate {
    public FileOutputStream fileOut = null ;
    public FileInputStream fileIn = null ;
    public FileOperate() throws Exception{
     fileOut = new FileOutputStream("bb.txt", true) ;
     fileIn = new FileInputStream("bb.txt") ;
    }

    public void write(Student stu) {
    try {
    ObjectOutputStream data = new ObjectOutputStream(
    fileOut);
    data.writeObject(stu);
    } catch (Exception event) {
    System.out.println("error for write!");
    }
    } public void read() {
    try {
    ObjectInputStream out = new ObjectInputStream(fileIn);
    Student stu2 = (Student) out.readObject();
    System.out.println(stu2.name + stu2.address + stu2.sex + stu2.date); } catch (Exception event) {
    System.out.println("error for read!");
    }
    }
    }public class BB {
    public static void main(String args[]) throws Exception{
    Student s1 = new Student("邬志刚", "1", "1", "1", "1", "1", "1", "1", "1",
    "1", "1");
    Student s2 = new Student("时磊", "2", "2", "2", "2", "2", "2", "2", "2",
    "2", "2");
    Student s3 = new Student("李广强", "3", "3", "3", "3", "3", "3", "3", "3",
    "3", "3");
    Student s4 = new Student("蒋雨辰", "4", "4", "4", "4", "4", "4", "4", "4",
    "4", "4");
    FileOperate file = new FileOperate();
    file.write(s1);
    file.write(s2);
    file.write(s3);
    file.write(s4);
    for (int i = 1; i <= 4; i++) {
    file.read();
    }
    }
    }
      

  3.   

    上面的代码  ptest 就是你的那个地址的代码  呵呵呵呵 这个人还蛮热情的。
    不知道能不能再问一下,在网络中  Socket   如何传序列化后的对象?然后对方怎么接受到?接收到就反序列化,就可以了么??