求一个序列化对象的例子!!!!!! 在写一个网络纸牌游戏需要序列化对象后再网络传输,但是看了类,不是很清楚。谁能给一个例子??小弟在此先谢过了! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 昨天回了一个贴在就是序列化对象的http://community.csdn.net/Expert/topic/4907/4907540.xml?temp=.5387384 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(); } }} 上面的代码 ptest 就是你的那个地址的代码 呵呵呵呵 这个人还蛮热情的。不知道能不能再问一下,在网络中 Socket 如何传序列化后的对象?然后对方怎么接受到?接收到就反序列化,就可以了么?? java数据结构转化 JEditorPane如何显示大文件(>100mb) HashSet会自动排序嘛?? 求一个四国算法: 我要执行一个SQL语句,然后把结果以TABLE的形式显示出来,请问应该怎样做?我是新人,谢谢 JBUILDER中使用SWING时自定义异常问题 applet 简单问题,在线所求答案。 什么是静态初始化? java中如何调用配置文件 .INI 文件 又没有人推荐一下 初学javase应该看点什么书 高手来 为什么这句也可以运行? 请大家看看这个程序
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();
}
}
}
不知道能不能再问一下,在网络中 Socket 如何传序列化后的对象?然后对方怎么接受到?接收到就反序列化,就可以了么??