//简单定义一个Student类
public class Student {
private String name;
private int age;
public Student(){}
public Student(String name,int age){
this.name = name;
this.age=age;
}
public void setName(String name){
this.name = name;
}
public void setAge(int age){
this.age = age;
}
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
//重写toString
@Override
public String toString(){
return ("my name is:"+this.name+" age is:"+this.age);
}
}
//main方法实现了将对象写入文件并读取出来
public static void main(String[] args) throws IOException{
DataOutputStream dot = new DataOutputStream(new FileOutputStream("hello.txt"));
Student stuW = new Student("walker",21);
//将此对象写入到文件中
dot.writeUTF(stuW.getName());
dot.writeInt(stuW.getAge());
dot.close();
//将对象从文件中读出
DataInputStream din = new DataInputStream(new FileInputStream("hello.txt"));
Student stuR = new Student();
stuR.setName(din.readUTF());
stuR.setAge(din.readInt());
din.close();
System.out.println(stuR);
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("hello.txt"));
Student stuW = new Student("walker",21);
oos.writeObject(stuW);
oos.close();
//从文件中读取该对象返回
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("hello.txt"));
Student stuR = (Student)ois.readObject();
System.out.println(stuR);
}
public ObjectOutputStream(OutputStream out)
public class Student implements Serializable {
String name;
int age;
Teacher t; //另外一个对象类型
public Student(){}
public Student(String name,int age,Teacher t){
this.name = name;
this.age=age;
this.t = t;
}
public void setName(String name){this.name = name;}
public void setAge(int age){this.age = age;}
public void setT(Teacher t){this.t = t;}
public String getName(){return this.name;}
public int getAge(){return this.age;}
public Teacher getT(){return this.t;}
}
public class Teacher implements Serializable {
String name;
public Teacher(String name){
this.name = name;
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
Teacher t = new Teacher("li");
Student stu1 = new Student("walker",21,t);
Student stu2 = new Student("yam",22,t);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("hello.txt"));
oos.writeObject(stu1);
oos.writeObject(stu2);
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("hello.txt"));
Student stuR1 = (Student)ois.readObject();
Student stuR2 = (Student)ois.readObject();
if (stuR1.getT() == stuR2.getT())
System.out.println("相同对象");
}
public class Student implements Serializable {
String name;
Teacher t;
}
public class Teacher implements Serializable {
String name;
Student stu;
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
Teacher t = new Teacher();
Student s =new Student();
t.name = "walker";
t.stu = s;
s.name = "yam";
s.t = t;
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("hello.txt"));
oos.writeObject(t);
oos.writeObject(s);
oos.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("hello.txt"));
Teacher tR = (Teacher)ois.readObject();
Student sR = (Student)ois.readObject();
if(tR == sR.t && sR == tR.stu)System.out.println("ok");
}
public class Student implements Serializable {
String name;
transient int age;
public String toString(){
return this.name + ":" + this.age;
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("hello.txt"));
Student stu = new Student();
stu.name = "walker";stu.age = 21;
oos.writeObject(stu);
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("hello.txt"));
Student stuR = (Student)ois.readObject();
System.out.println(stuR);
}
//改动过的student类,main方法没有改动,大家可以往上看
public class Student implements Serializable {
String name;
transient int age;
private void writeObject(ObjectOutputStream oos) throws IOException {
oos.defaultWriteObject();
oos.writeInt(25);
}
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
ois.defaultReadObject();
age = ois.readInt();
}
public String toString(){
return this.name + ":" + this.age;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有