本文主要是介绍IO流 —对象流,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package com.company;
import java.io.Serializable;
/**
*先创建一个学生类
* 注意实现Sreializable
*/
public class Student implements Serializable {
private String name;
private int age;
public Student(){
}
public Student(String name,int age) {
super();
this.age = age;
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
package com.company;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
/**
* 使用OutputStream实现对象的序列化
*/
public class Demo1 {
public static void main(String[] args) throws Exception{
FileOutputStream fis = new FileOutputStream("E:\\Demo1.txt");
ObjectOutputStream oos = new ObjectOutputStream(fis);
//序列化
Student s1 = new Student("张三",23);
oos.writeObject(s1);
oos.close();
}
}
这篇关于IO流 —对象流的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!