序列化:将指定对象,以"字节流"的方式写入一个文件或网络中。
反序列化:从一个文件或网络中,以"字节流"的方式读取到对象。
package com.ztt.Demo01;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
public class demo16 {
public static void main(String[] args) {
ArrayList<String> list=new ArrayList<String>();
list.add("张甜甜");
list.add("张文印");
list.add("甜甜");
//序列化:将"对象"写入至文件
try(ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("D:\\test\\tt\\list.bin"));){
//将list集合对象,写入list.bin文件中
oos.writeObject(list);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
输出结果:
package com.ztt.Demo01;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
public class demo17 {
public static void main(String[] args) {
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("D:\\test\\tt\\list.bin"))) {
// 读取对象并尝试转换为ArrayList<String>
ArrayList<String> list = (ArrayList<String>) in.readObject();
System.out.println(list);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
} catch (ClassCastException e) {
e.printStackTrace();
}
}
}
输出结果:
[张甜甜, 张文印, 甜甜]
package com.ztt.Demo01;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class demo18 {
public static void main(String[] args) {
Order order=new Order("SN002",1234.56,"AX59",0.5);
//序列化时,该对象的类型,必须实现Serializable接口
try(ObjectOutputStream oos=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("D:\\test\\tt\\order.ser")))){
//写入对象
oos.writeObject(order);
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Order implements Serializable{
/*
* 序列化版本号
*/
private static final long serialVersionUID=6607463456938102524L;
private String orderNo;//订单编号
private double pay;//支付金额
private double sale;//折扣
//transient关键字修饰的成员变量,在序列化时,不会被写入
private transient String validateCode;//验证码
//无参构造方法
public Order() {
this.orderNo="SN00X";
this.pay=0.1;
}
//有参构造方法
public Order(String orderNo, double pay,String validateCode,double sale) {
this .orderNo = orderNo ;
this.pay = pay;
this.validateCode=validateCode;
this.sale=sale;
}
public Order(String orderNo) {
this.orderNo=orderNo;
}
@Override
public String toString() {
return "Order [orderNo=" + orderNo + ", pay=" + pay + ", sale=" + sale + ", validateCode=" + validateCode
+ "]";
}
public String getOrderNo() {
return orderNo;
}
public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
}
public double getPay() {
return pay;
}
public void setPay(double pay) {
this.pay = pay;
}
}
package com.ztt.Demo01;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
public class demo19 {
public static void main(String[] args) {
//反序列化:从文件中读取一个对象
try(ObjectInputStream in =new ObjectInputStream(
new BufferedInputStream(
new FileInputStream("d:\\test\\tt\\order.ser")))){
Order order =(Order)in.readObject();
System.out.println(order);
} catch (ClassNotFoundException| IOException e) {
e. printStackTrace();
}
}
}
输出结果:
Order [orderNo=SN002, pay=1234.56, sale=0.5, validateCode=null]
注意:
需要进行序列化操作的对象,必须实现Serializable接口,使用transient关键字修饰的成员变量,在序列化时,不会被写入;在反序列化时,不会被读取。
使用场景:本地缓存,在网络环境下,将对象以流的方式,进行传输。
例如:工作列表(备忘录):
package com.ztt.Demo01;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Scanner;
//工作列表(备忘录)
public class demo20 {
static ArrayList<String> todoList;
static {
//程序启动时,从文件中,反序列化得到一个todoList集合对象
try(ObjectInputStream in=new ObjectInputStream(new BufferedInputStream(new FileInputStream("D:\\test\\tt\\todo.bin")));){
//读取对象
Object data=in.readObject();
if(data==null) {
todoList=new ArrayList<String>();
}else {
todoList=(ArrayList<String>)data;
}
} catch (FileNotFoundException e) {
todoList=new ArrayList<String>();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println("程序启动完毕!");
System.out.println("当前备忘录工作列表中包含:");
System.out.println(todoList);
System.out.println("是否添加新的备忘?");
try(Scanner input=new Scanner(System.in)){
String line=input.nextLine();
while(line.equalsIgnoreCase("yes")) {
//添加一个工作备忘
String work=input.nextLine();
todoList.add(work);
System.out.println("是否继续添加?");
line=input.nextLine();
}
}
System.out.println("添加完毕!");
System.out.println("当前备忘录工作列表中包含:");
System.out.println(todoList);
//程序退出时,将todoList集合对象,序列化至文件
try(ObjectOutputStream out=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("D:\\test\\tt\\todo.bin")));){
out.writeObject(todoList);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.ztt.Demo01;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
public class demo19 {
public static void main(String[] args) {
//反序列化:从文件中读取一个对象
try(ObjectInputStream in =new ObjectInputStream(
new BufferedInputStream(
new FileInputStream("d:\\test\\tt\\order.ser")))){
Order order =(Order)in.readObject();
System.out.println(order);
} catch (ClassNotFoundException| IOException e) {
e. printStackTrace();
}
}
}
输出结果:
程序启动完毕!
当前备忘录工作列表中包含:
[AAA, BBB]
是否添加新的备忘?
yes
CCC
是否继续添加?
no
添加完毕!
当前备忘录工作列表中包含:
[AAA, BBB, CCC]
Properties
- *.properties格式的文件,内容是Key-Value键值对格式(key=value)
- 注释使用#
- 使用Properties类完成*properties格式文件的读取和写入,读取使用 load(),写入使用store()
- Properties类本质其实是一个Map集合,是Hashtable类的子类。
package com.ztt.Demo01;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
//properties 类型的文件: key-value键值对文件
//读取
public class demo21 {
public static void main(String[] args) {
try(FileInputStream fis=new FileInputStream("D:\\test\\tt\\config.properties")){
//创建Properties类型的对象props,用于读取* .properties类型的文件
Properties props=new Properties();
//读取
props.load(fis);
//根据key获取value
System.out.println(props.get("029"));
System.out.println(props);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
输出结果:
西安
{2448=杭州, 029=西安, 020=上海}
package com.ztt.Demo01;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class demo22 {
public static void main(String[] args) {
//读取classpath路径下的*.properties文件
//当前类所在的位置: Demo22. class . getResourceAsstream( " config. properties" )
//当前ClassPath 下的指定位置: Demo22. class. getResourceAsstream( "/com/ .. ./config . properties'
try(InputStream in=demo22.class.getResourceAsStream("/com/ztt/Demo01/config.properties")){
Properties props=new Properties();
props.load(in);
System.out.println(props);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
输出结果:
{2448=杭州, 025=成都, 029=西安, 020=上海, 010=北京}
package com.ztt.Demo01;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
//properties文件的写入操作
public class demo23 {
public static void main(String[] args) {
Properties props=new Properties();
props.put("version", "v0.0001");
props.put("active-code", "asdfghjklzxcvbnmqwertyuiop");
try(FileOutputStream fos=new FileOutputStream("D:\\test\\tt\\app.properties")){
//将键值对,通过输出流写入指定文件,并添加注释
props.store(fos, "My application basic properties data.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
输出结果: