Properties类

介绍:Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。

Properties类特点:

1、Hashtable的子类,map集合中的方法都可以用。

2、该集合没有泛型。键值都是字符串。

3、它是一个可以持久化的属性集。键值可以存储到集合中,也可以存储到持久化的设备(硬盘、U盘、光盘)上。键值的来源也可以是持久化的设备。

4、有和流技术相结合的方法。

方法如下:

IO—》Properties类&序列化流与反序列化流-LMLPHP

①public Object setProperty(String key, String value)调用 Hashtable 的方法 put。

②public Set<String> stringPropertyNames()返回此属性列表中的键集。

③public String getProperty(String key)用指定的键在此属性列表中搜索属性。

举例:

1、读取:

public static void method1() throws IOException{
//从properties文件中读取键值对
Properties pro=new Properties();
//明确数据源
FileInputStream fis=new FileInputStream("e:\\test\\demo1.properties");
pro.load(fis);
fis.close();
//遍历
Set<String> set=pro.stringPropertyNames();
for(String s:set){
System.out.println(s+"..."+pro.getProperty(s));
}
}

2、存储

public static void method2() throws IOException{
//通过properties向文件中写键值对
//明确目的地
FileOutputStream fos=new FileOutputStream("e:\\test\\demo2.properties");
Properties pro=new Properties();
pro.setProperty("name", "baba");
pro.setProperty("age", "85");
pro.store(fos, "你好");
}

注意:使用字符流FileReader就可以完成文件中的中文读取操作了

序列化流与反序列化流

用于从流中读取对象的

操作流 ObjectInputStream    称为 反序列化流

用于向流中写入对象的操作流 ObjectOutputStream   称为 序列化流

特点:用于操作对象。可以将对象写入到文件中,也可以从文件中读取对象。

1、对象序列化流ObjectOutputStream

ObjectOutputStream 将 Java 对象的基本数据类型和图形写入 OutputStream。可以使用 ObjectInputStream 读取(重构)对象。通过在流中使用文件可以实现对象的持久存储。

注意:只能将支持 java.io.Serializable 接口的对象写入流中

IO—》Properties类&amp;序列化流与反序列化流-LMLPHP

IO—》Properties类&amp;序列化流与反序列化流-LMLPHP

演示:

public static void method3() throws IOException{
Person p=new Person("张三", 18);
//明确目的地
FileOutputStream fos=new FileOutputStream("e:\\test\\person.txt");
//创建序列化流
ObjectOutputStream oos=new ObjectOutputStream(fos);
//写入对象
oos.writeObject(p);
//释放资源
oos.close();
}

2、对象反序列化流ObjectInputStream

ObjectInputStream 对以前使用 ObjectOutputStream 写入的基本数据和对象进行反序列化。支持 java.io.Serializable接口的对象才能从流读取。

IO—》Properties类&amp;序列化流与反序列化流-LMLPHP

IO—》Properties类&amp;序列化流与反序列化流-LMLPHP

演示:

//反序列化
public static void method4() throws IOException, ClassNotFoundException{
//明确数据源
FileInputStream fis=new FileInputStream("e:\\test\\person.txt");
//创建反序列化对象
ObjectInputStream ois=new ObjectInputStream(fis);
//反序列化
Person p=(Person)ois.readObject();
System.out.println(p);
}

3、序列化接口

当一个对象要能被序列化,这个对象所属的类必须实现Serializable接口。否则会发生异常NotSerializableException异常。

同时当反序列化对象时,如果对象所属的class文件在序列化之后进行的修改,那么进行反序列化也会发生异常InvalidClassException。发生这个异常的原因如

下:

①该类的序列版本号与从流中读取的类描述符的版本号不匹配

②该类包含未知数据类型

③该类没有可访问的无参数构造方法

Serializable标记接口。该接口给需要序列化的类,提供了一个序列版本号。serialVersionUID. 该版本号的目的在于验证序列化的对象和对应类是否版本匹配。

public class Person implements Serializable {
//给类显示声明一个序列版本号。
private static final long serialVersionUID = 1L;
private String name;
private int age;
public Person() {
super();
}
public Person(String name, int age) {
super();
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;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}

瞬态关键字transient

当一个类的对象需要被序列化时,某些属性不需要被序列化,这时不需要序列化的属性可以使用关键字transient修饰。只要被transient修饰了,序列化时这个属

性就不会序列化了。

同时静态修饰也不会被序列化,因为序列化是把对象数据进行持久化存储,而静态的属于类加载时的数据,不会被序列化。

public class Person implements Serializable{
private String name;
private transient int age;//瞬态关键字(阻止属性不被序列化)
private static final long serialVersionUID=123456789L;
public Person(String name, int age) {
super();
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;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
05-07 15:17