学习目录
一、对象处理流☆☆☆
1.基本概念
对象处理流(包装流):提供了对基本类型或对象类型的序列化和反序列化的方法
ObjectOutputStream:提供序列化功能
ObjectInputStream: 提供 反序列化功能
序列化和反序列化
- 序列化就是在保存数据时,保存数据的值和数据类型
- 反序列化就是在恢复数据时,恢复数据的值和数据类型
- 需要让某个对象支持序列化机制,则必须让其类是可序列化的,为了让某个类是可序列化的,该类必须实现如下两个接口之一:Serializable(默认,因为这是一个标记接口)或者Externalizable
注意:如果想序列化某个类的对象,则该类必须实现上面两个接口中的一个
2.ObjectOutputStream
快速入门
要求:使用ObjectOutputStream序列化 基本数据类型和一个Dog对象(name,age),并保存到data.dat文件中
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class test {
public static void main(String[] args) throws IOException {
//序列化后,保存的文件格式是按照他的格式来保存的
String Path = "F:\\data.dat";
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(Path));
//序列化数据到 F:\data.dat
oos.writeInt(100); //将int自动装箱为Integer,而Integer则实现了Serializable接口
oos.writeBoolean(true); //将boolean自动装箱为Boolean,而Boolean则实现了Serializable接口
oos.writeChar('a');
oos.writeDouble(9.5);
oos.writeUTF("你好java");
//保存一个dog对象
oos.writeObject(new Dog("小花",2));
oos.close();
System.out.println("序列化完成!");
}
}
//如果需要序列化某个类的对象,则需要实现Serializable或Externalizable接口
//如果需要序列化某个类的对象,则需要实现Serializable或Externalizable接口
class Dog implements Serializable {
String name;
int age;
public Dog(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;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
3.ObjectInputStream
快速入门
要求:使用对上面案例进行反序列化,并打印在控制台
注意:读取(反序列化)的顺序需要和你保存数据(序列化)的数据顺序一致,否则会出现异常
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
public class test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//序列化后,保存的文件格式是按照他的格式来保存的
String Path = "F:\\data.dat";
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(Path));
//读取(反序列化)的顺序需要和你保存数据(序列化)的数据顺序一致,否则会出现异常
System.out.println(ois.readInt());
System.out.println(ois.readBoolean());
System.out.println(ois.readChar());
System.out.println(ois.readDouble());
System.out.println(ois.readUTF());
//获取Dog的对象,但不能使用Dog中的方法,必须进行向下转型
Object dog = ois.readObject();
System.out.println("运行类型="+dog.getClass()); //运行类型为 Dog,由Object -> Dog
System.out.println("dog信息="+dog);
//如果想调用Dog方法,则需要向下转型
Dog dog1 = (Dog)dog;
System.out.println(dog1.getName());
System.out.println(dog1.getAge());
//底层会自动关闭字节流
ois.close();
}
}
//如果需要序列化某个类的对象,则需要实现Serializable或Externalizable接口
class Dog implements Serializable {
String name;
int age;
public Dog(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;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
4.注意事项
- 对象处理流读写顺序要一致
- 要求实现序列化或反序列化对象,需要实现Serializable
- 序列化的类中建议添加SerialVersionUID,为了提高版本的兼容性
- 序列化对象时,默认将里面所有属性都进行序列化(除了static或transient修饰的成员,不能被序列化)
- 序列化对象时,如果该对象里面属性有对象类型,该属性也需要实现序列化接口
- 序列化具备可继承性,也就是如果某类已经实现了序列化,则它的所有子类也已经默认实现序列化
细节四举例说明
序列化
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class test {
public static void main(String[] args) throws IOException {
//序列化后,保存的文件格式是按照他的格式来保存的
String Path = "F:\\data1.dat";
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(Path));
//序列化数据到 F:\data1.dat
oos.writeInt(100); //将int自动装箱为Integer,而Integer则实现了Serializable接口
//保存一个dog对象
oos.writeObject(new Dog("小花",3,"red","中国"));
oos.close();
System.out.println("序列化完成!");
}
}
//如果需要序列化某个类的对象,则需要实现Serializable或Externalizable接口
class Dog implements Serializable {
String name;
int age;
static String color;
transient String nation;
public Dog(String name, int age,String color, String nation) {
this.name = name;
this.age = age;
this.nation = nation;
this.color = color;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
", nation='" + nation + '\'' +
", color='" + color + '\'' +
'}';
}
}
反序列化
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
public class test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//序列化后,保存的文件格式是按照他的格式来保存的
String Path = "F:\\data1.dat";
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(Path));
//读取(反序列化)的顺序需要和你保存数据(序列化)的数据顺序一致,否则会出现异常
System.out.println(ois.readInt());
//获取Dog的对象,但不能使用Dog中的方法,必须进行向下转型
Object dog = ois.readObject();
System.out.println("dog信息="+dog);
ois.close();
System.out.println("反序列化完成!");
}
}
//如果需要序列化某个类的对象,则需要实现Serializable或Externalizable接口
class Dog implements Serializable {
String name;
int age;
static String color;
transient String nation;
public Dog(String name, int age,String color, String nation) {
this.name = name;
this.age = age;
this.nation = nation;
this.color = color;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
", nation='" + nation + '\'' +
", color='" + color + '\'' +
'}';
}
}
由上面输出信息可以看出,nation和color的值都是null,而我们初始化的时候是赋值了的,所以static和transient修饰的属性是不能被序列化的
二、标准输入输出流
父类 | 运行类型 | 默认设备 | |
---|---|---|---|
System.in 标准输入 | InputStream | BufferedInputStream | 键盘 |
System.out 标准输出 | PrintStream | PrintStream | 显示器 |
import java.io.InputStream;
import java.io.PrintStream;
public class test {
public static void main(String[] args) {
//编译类型为InputStream
InputStream in = System.in;
//运行类型为BufferedInputStream
System.out.println(in.getClass());
//编译类型为PrintStream
PrintStream out = System.out;
//运行类型为PrintStream
System.out.println(out.getClass());
}
}
三、转换流(字节流转为字符流)
在输入输出流的同时,会因为编码的不同导致文件的读取或写入出现乱码问题
基本介绍:
- InputStreamReader是Reader的子类,可以将**InputStream(字节流)**包装成(转换)Reader(字符流)
- OutputStreamWriter是Writer的子类,实现将OutputStream(字节流)包装成Writer(字符流)
- 当处理纯文本数据时,如果使用字符流效率更高,并且可以有效解决中文问题,所以建议将字节流转换成字符流
- 可以在使用时指定编码格式(比如 utf-8,gbk,gb2312,ISO8859-1 等)
1.InputStreamReader
快速入门:
读取test.txt中的内容
import java.io.*;
public class test {
public static void main(String[] args) throws IOException {
String Path = "F:\\test.txt";
//指定gbk编码,将字节流FileInputStream转成字符流InputStreamReader
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(Path),"gbk"));
String s = br.readLine();
System.out.println("读取内容="+s);
br.close();
}
}
2.OutputStreamWriter
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class test {
public static void main(String[] args) throws IOException {
String Path = "F:\\test5.txt";
//把FileOutputStream字节流,转成字符流OutputStreamWriter
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(Path), "gbk");
osw.write("你好java");
osw.close();
}
}
四、打印流
打印流分为两种:PrintStream(字节流)和PrintWriter(字符流)
打印流只有输出流,没有输入流
1.PrintStream
import java.io.IOException;
import java.io.PrintStream;
public class test {
public static void main(String[] args) throws IOException {
PrintStream out = System.out;
//在默认情况下,PrintStream输出数据的位置是标准输出,即控制台
out.println("你好");
//方法二:因为print的底层是的write,所有我们可以直接调用write进行打印/输出
out.write("你好java".getBytes());
//方法三:也可以去修改打印流输出的位置/设备
System.setOut(new PrintStream("F:\\test6.txt"));
System.out.println("你好C++"); //此时打印的内容在F:\test6.txt
out.close();
}
}
2.PrintWriter
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class test {
public static void main(String[] args) throws IOException {
PrintWriter printWriter = new PrintWriter(new FileWriter("F:\\test7.txt"));
printWriter.print("你好scala"); //打印到F:\\test7.txt里面
printWriter.close();
}
}
五、Properties类
1.基本介绍
Properties类专门用于读写配置文件的集合类
配置文件的格式:
键=值
键=值
…
注意:键值对不需要有空格,值不需要用引号引起来。因为默认类型是String
2.Properties常见的方法
方法名 | 功能 |
---|---|
load | 加载配置文件的键值对到Properties对象 |
list | 将数据显示到指定设备 |
getProperty(key) | 根据键获取值 |
setProperty(key,value) | 设置键值对到Properties对象 |
store | 将Properties中的键值对存储到配置文件,在idea中,保存信息到配置文件,如果含有中文,则会存储为unicode码 |
3.基本使用
1.使用Properties类完成对Properties文件的读取
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
public class test {
public static void main(String[] args) throws IOException {
//要求一
//创建Properties对象
Properties properties = new Properties();
//加载指定配置文件
properties.load(new FileReader("src\\test.properties"));
//K-V输出打印
properties.list(System.out);
System.out.println("==============");
//根据key获取对应的值
String root = properties.getProperty("root");
String name = properties.getProperty("name");
String password = properties.getProperty("password");
System.out.println("用户名:"+root);
System.out.println("姓名:"+name);
System.out.println("密码:"+password);
}
}
2.使用Properties类添加key-val到新的Properties文件中
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class test {
public static void main(String[] args) throws IOException {
//创建Properties对象
Properties properties = new Properties();
properties.setProperty("charset","utf-8");
properties.setProperty("name1","张三"); //注意,这里的中文会被保存为Unicode码值
//第二个参数相当于一个注释,在文件的最上方,如果不写的话,赋值为null即可
properties.store(new FileOutputStream("src\\test1.properties"),null);
properties.list(System.out);
}
}
3.使用Properties类完成对Properties文件的读取,并修改某个key-val
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
public class test {
public static void main(String[] args) throws IOException {
//创建Properties对象
Properties properties = new Properties();
properties.load(new FileReader("src\\test1.properties"));
//如果没有key就是创建
properties.setProperty("password","456");
properties.list(System.out);
}
}