对象序列化技术
- 对象序列化:就是把Java对象数据直接存储到文件中去。 对象 => 文件中
- 对象反序列化:就是把Java对象的文件数据恢复到Java对象中。 文件中 => 对象
字节输入流 | 字节输出流 | 字符输入流 | 字符输出流 |
---|---|---|---|
InputStream | OutputStream | Reader | Writer (抽象类) |
FileInputStream | FileOutputStream | FileReader | FileWriter(实现类) |
BufferedInputStream | BufferedOutputStream | BufferedReader | BufferedWriter(实现类,缓冲流) |
InputStreamReader | OutputStreamWriter(字符转换流) | ||
ObjectInputStream | ObjectOutputStream | 对象序列化流 |
-
对象序列化流(对象字节输出流):ObjectOutputStream
-
– 作用:把内存中的Java对象数据保存到文件中去。
-
– 构造器: public ObjectOutputStream(OutputStream out)
-
– 序列化方法:public final void writeObject(Object obj)
-
public static void main(String[] args) throws Exception { // 1.创建User用户对象 User user = new User("tsgz", "003197", "铁扇公主"); // 2.创建低级的字节输出流通向目标文件 OutputStream os = new FileOutputStream("Day10Demo/src/obj.dat"); // 3.把低级的字节输出流包装成高级的对象字节输出流ObjectOutputStream ObjectOutputStream oos = new ObjectOutputStream(os); // 4.通过对象字节输出流序列化对象: oos.writeObject(user); // 6.释放资源 oos.close(); System.out.println("序列化对象成功~~~~"); }
-
注意:对象如果想参与序列化,对象必须实现序列化接口 implements Serializable ,否则序列化失败!
-
-
对象反序列化(对象字节输入流):ObjectInputStream
-
– 作用:读取序列化的对象文件恢复到Java对象中。
-
– 构造器:public ObjectInputStream(InputStream is)
-
– 方法:public final Object readObject()
-
public static void main(String[] args) throws Exception { // 1.定义一个低级的字节输入流通向源文件 InputStream is = new FileInputStream("Day10Demo/src/obj.dat"); // 2.把字节输入流包装成高的对象字节输入流 ObjectInputStream ois = new ObjectInputStream(is); // 3.反序列化 User user = (User) ois.readObject(); System.out.println(user); System.out.println("反序列化完成!"); }
-
如果一个字段不想参数序列化:使用transient修饰该成员变量,它将不参与序列化!
-
加入序列化版本号:
private static final long serialVersionUID = 2L
,必须序列化使用的版本号和反序列化使用的版本号一致才可以正常反序列化!否则报错!
-
打印流PrintStream / PrintWriter.
-
打印流的作用:
- 1.可以方便,快速的写数据出去。
- 2.可以实现打印啥出去,就是啥出去。
-
打印流的构造器:
- public PrintStream(OutputStream os):
- public PrintStream(String filePath):
-
小结:
- 打印流可以方便,且高效的打印各种数据。
- PrintStream不光可以打印数据,还可以写"字节数据"出去。
- PrintWriter不光可以打印数据,还可以写"字符数据"出去。
-
public static void main(String[] args) throws Exception { // 1.打印流PrintStream //OutputStream os = new FileOutputStream("Day10Demo/src/dlei08.txt"); //PrintStream ps = new PrintStream(os); PrintStream ps = new PrintStream("Day10Demo/src/dlei08.txt"); //PrintWriter pw = new PrintWriter("Day10Demo/src/dlei08.txt"); ps.println(97); // 写97 ps.println(110); // 写110 ps.println("我在黑马快乐的调皮~~"); ps.println(99.8); ps.println(false); ps.println('徐'); // 写字节数据出去 // ps.write("我爱你".getBytes()); ps.close(); }
-
打印流改变输出的流向:重定向。
-
public static void main(String[] args) throws Exception { System.out.println("==itheima0=="); PrintStream ps = new PrintStream("Day10Demo/src/log.txt"); System.setOut(ps); // 让系统的输出流向打印流。 System.out.println("==itheima1=="); System.out.println("==itheima2=="); System.out.println("==itheima3=="); System.out.println("==itheima4=="); System.out.println("==itheima5=="); }
-
Properties
-
目标:Properties的概述和使用(框架底层使用,了解这个技术即可)。(保存数据到属性文件)
-
Properties:属性集对象。
-
其实就是一个Map集合。也就是一个键值对集合。但是我们一般不会当集合使用,因为有HashMap。
-
Properties核心作用:Properties代表的是一个属性文件,可以把键值对的数据存入到一个属性文件中去。
-
属性文件:后缀是.properties结尾的文件,里面的内容都是 key=value。
-
大家在后期学的很多大型框架技术中,属性文件都是很重要的系统配置文件。
-
users.properties admin=123456 dlei=dlei
-
需求:使用Properties对象生成一个属性文件,里面存入用户名和密码信息。
-
Properties的方法:
-
– public Object setProperty(String key, String value) : 保存一对属性。
-
– public String getProperty(String key) :使用此属性列表中指定的键搜索属性值
-
– public Set stringPropertyNames() :所有键的名称的集合
-
– public void store(OutputStream out, String comments):保存数据到属性文件中去
-
– public void store(Writer fw, String comments):保存数据到属性文件中去
-
public static void main(String[] args) throws Exception { // a.创建一个属性集对象:Properties的对象。 Properties properties = new Properties(); properties.setProperty("admin", "123456"); properties.setProperty("dlei", "101333"); System.out.println(properties); // b.把属性集对象的数据存入到属性文件中去(重点) OutputStream os = new FileOutputStream("Day10Demo/src/users.properties"); /** * 参数一:被保存数据的输出管道 * 参数二:保存心得。就是对象保存的数据进行解释说明! */ properties.store(os, "i am very happy!!我快乐的保存了用户数据!"); }
-
-
小结:属性集对象Properties实际上是一个Map集合,可以实现把键值对数据保存到属性文件中去!!
-
Properties读取属性文件中的键值对信息。(读取)
-
Properties的方法:
-
– public Object setProperty(String key, String value) : 保存一对属性。
-
– public String getProperty(String key) :使用此属性列表中指定的键搜索属性值
-
– public Set stringPropertyNames() :所有键的名称的集合
-
– public void store(OutputStream out, String comments):保存数据到属性文件中去
-
– public synchronized void load(InputStream inStream):加载属性文件的数据到属性集对象中去
-
– public synchronized void load(Reader fr):加载属性文件的数据到属性集对象中去
-
public static void main(String[] args) throws Exception { // a.创建一个属性集对象:Properties的对象。 Properties properties = new Properties(); properties.setProperty("admin", "123456"); properties.setProperty("dlei", "101333"); System.out.println(properties); // b.把属性集对象的数据存入到属性文件中去(重点) OutputStream os = new FileOutputStream("Day10Demo/src/users.properties"); /** * 参数一:被保存数据的输出管道 * 参数二:保存心得。就是对象保存的数据进行解释说明! */ properties.store(os, "i am very happy!!我快乐的保存了用户数据!"); }
-
-
小结:属性集对象可以加载读取属性文件中的数据!
-
ps:b站课程《黑马程序员Java13天进阶》根据官方笔记结合自身情况整理的笔记
视频链接