序列化,反序列化,properties,SequenceInputScream
序列化和反序列化
(1)概述
所谓的序列化:就是把对象通过流的方式存储到文件中.注意:此对象 要重写Serializable 接口才能被序列化
反序列化:就是把文件中存储的对象以流的方式还原成对象
序列化流: ObjectOutputStream
反序列化流: ObjectInputStream
像这样一个接口中如果没有方法,那么这样的接口我们将其称之为标记接口(用来给类打标记的,相当于猪肉身上盖个章)
一个对象可以被序列化的前提是这个对象对应的类必须实现Serializable接口
(2)举例
//接口中没有任何方法,那么这个接口我们称之为标记接口
//作用给这个类打个标记,让JVM支持序列化
class Student implements Serializable,Cloneable {
private static final long serialVersionUID = -7602640005373026150L;
//public static final long serialVersionUID = 42L;
private String name;
//transient 某个成员变量不想序列化可以使用transient来修饰一下
public transient int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
}
public class MyTest {
public static void main(String[] args) throws IOException, CloneNotSupportedException, ClassNotFoundException {
readObj();
}
private static void readObj() throws IOException, ClassNotFoundException {
ObjectInputStream objin = new ObjectInputStream(new FileInputStream("student.txt"));
Object obj = objin.readObject();
Student student= (Student) obj;
System.out.println(student.getName());
System.out.println(student.getAge());
}
private static void writeObj() throws IOException {
Student student = new Student("zhangsan",23);
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("student.txt"));
out.writeObject(student);
out.close();
}
}
(3)如何让对象的成员变量不被序列化
使用transient关键字声明不需要序列化的成员变量
propertise
(1)概述
Properties 类表示了一个持久的属性集。
Properties 可保存在流中或从流中加载。
属性列表中每个键及其对应值都是一个字符串。
Properties父类是Hashtable
Properties属于双列集合,这个集合中的键和值都是字符串 Properties不能指定泛型
(2)特殊功能
public Object setProperty(String key,String value)
public String getProperty(String key)
public Set stringPropertyNames() 规定键值是String类型
(3)举例
public class Demo {
public static void main(String[] args)throws IOException {
// Properties 属性集合,经常用它来读写配置文件 属于双列集合
Properties properties = new Properties();//他规定了键值 是String类型
//用它特有的方法,来存储键值
properties.setProperty("陈羽凡","白百合");
String value = properties.getProperty("陈羽凡");
System.out.println(value);
//参数2,默认值,如果键没有找到对应的值,就返回默认值
String property = properties.getProperty("陈羽凡1", "李小璐");
System.out.println(property);
}
}
结果:
白百合
李小璐
(4)store()功能
public class MyTest3 {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
properties.setProperty("username","王五");
properties.setProperty("password","654321");
//把 属性集合中的数据,保存到配置文件中
properties.store(new FileWriter("hehe.properties"),null);
}
}
这时会创建一个文件,文件的内容是:
#Thu Jul 25 15:20:19 CST 2019
password=654321
username=王五
(5)load()功能
public class Demo {
public static void main(String[] args)throws IOException {
Properties properties = new Properties();
//读取配置文件
//要求配置文件键值用等号 = 连接
properties.load(new FileReader("hehe.properties"));
System.out.println(properties);
}
}
这时在控制台输出:
{password=654321, username=王五}
SequenceInputScream
(1)概述:
表示其他输入流的逻辑串联。
它从输入流的有序集合开始,
并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,
依次类推,直到到达包含的最后一个输入流的文件末尾为止
(2)构造方法
1.SequenceInputStream(InputStream s1, InputStream s2)
通过记住这两个参数来初始化新创建的 SequenceInputStream(将按顺序读取这两个参数,先读取 s1,然后读取 s2),
以提供从此 SequenceInputStream 读取的字节。
2.SequenceInputStream(Enumeration<? extends InputStream> e)
通过记住参数来初始化新创建的 SequenceInputStream,该参数必须是生成运行时类型为 InputStream 对象的 Enumeration 型参数。