1、Properties 没有泛型。也是哈希表集合,无序集合。{a=1,b=2,c=3}
2、 读取文件中的数据,并保存到集合 (Properties方法:stringPropertyNames()返回此属性列表中的键集)
public class PropertiesDemo {
public static void main(String[] args) throws IOException {
//1,创建集合
Properties prop = new Properties();
//2,创建流对象
FileReader in = new FileReader("g:\\ivan.txt");
//FileReader in = new FileReader("prop.properties");
//3,把流所对应文件中的数据 读取到集合中
prop.load(in);
//4,关闭流
in.close();
//5,显示集合中的数据
System.out.println(prop); }
}
3、序列化流:用于从流中读取对象的操作流 ObjectOutputStream。
反序列化流:用于向流中写入对象的操作流 ObjectInputStream。
=》? 特点:用于操作对象。可以将对象写入到文件中,也可以从文件中读取对象。
4、ObjectOutputStream。注意:只能将支持 java.io.Serializable 接口的对象写入流中。
import java.io.Serializable;//序列化接口 public class Person implements Serializable {
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 + "]";
}
}
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream; public class ObjectStreamDemo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
/*
* 将一个对象存储到持久化(硬盘)的设备上。
*/
writeObj();//对象的序列化。
}
public static void writeObj() throws IOException {
//1,明确存储对象的文件。
FileOutputStream fos = new FileOutputStream("g:\\oos.dat");
//2,给操作文件对象加入写入对象功能。
ObjectOutputStream oos = new ObjectOutputStream(fos);
//3,调用了写入对象的方法。
oos.writeObject(new Person("wangcai",20));
System.out.println(new Person("wangcai",20).toString());
//关闭资源。
oos.close();
}
}
5、ObjectInputStream(反序列化流--类) 对以前使用 ObjectOutputStream 写入的基本数据和对象进行反序列化。支持 java.io.Serializable接口的对象才能从流读取。
public class ObjectStreamDemo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
readObj();//对象的反序列化。
}
public static void readObj() throws IOException, ClassNotFoundException { //1,定义流对象关联存储了对象文件。
FileInputStream fis = new FileInputStream("g:\\oos.dat"); //2,建立用于读取对象的功能对象。
ObjectInputStream ois = new ObjectInputStream(fis);//牛逼!
Person obj = (Person)ois.readObject(); //类型强转为Person ,readObject()方法:从 ObjectInputStream 中读取对象。
System.out.println(obj.toString());
}
}
5、Serializable标记型接口。该接口给需要序列化的类,提供了一个序列版本号。serialVersionUID. 该版本号的目的在于验证序列化的对象和对应类是否版本匹配。不具备别的方法和功能。
6、transient 瞬态修饰符,用于不被序列化的类中成员变量。注意static修饰符下的成员变量 public static int a,与类的存储位置是不一样的。所以需要序列化写入类时候,a的 默认值为0。
静态修饰也不会被序列化,因为序列化是把对象数据进行持久化存储,而静态的属于类加载时的数据,不会被序列化。
7、做一个终身不变的序列号。复制粘贴。static long SerializableUID = 13L;
8、store( OutputStream out , String message ) 把集合中的数据,写入到流所对应的文件中。 store( Writer out , String message) 把集合中的数据,写入到流所对应的文件中