《IO流(二)》
目录
一、转换流
字节是文件流操作最小单位,字符则是文本操作最佳选择,Reader与Writer提供了两个子类作为转换流,即InputStreamReader与OutputStreamWriter。
OutputStreamWriter
try {
OutputStream os = new FileOutputStream("./rootDir/osw.txt");
//转换输出流需要嵌套一个OutputStream对象,同时可指定写出的字符集
OutputStreamWriter osw = new OutputStreamWriter(os, StandardCharsets.UTF_8);
osw.write("你在桥上看风景,\r\n我在桥上看你,\r\n明月装饰了你的窗子,\r\n你装饰了我的梦。");
//资源关闭根据引用顺序“倒着关”
osw.flush();
osw.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("程序执行完毕!");
执行结果
InputStreamReader
try {
InputStream is = new FileInputStream("./rootDir/osw.txt");
InputStreamReader isr = new InputStreamReader(is);
int x = isr.read();
while (x != -1) {
System.out.print((char) x);
x = isr.read();
}
isr.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
执行结果
二、高层流
高层流(嵌套流),使用时需传入底层流(如上述InputStreamReader、OutputStreamWriter),高层流不光封装了更多的操作方法,同时提高了读写效率,常用的高层流有以下几个:
- 缓冲流:BufferedInputStream、BufferedOutputStream、BufferedReader、BufferedWriter;
- 打印流:PrintStream、PrintWriter;
- 数据流:DataInputStream、DataOutputStream;
- 对象流:ObjectInputStream、ObjectOutputStream。
其中缓冲流高效利用缓冲区提高效率,打印流提供print、println等方法方便文本打印,数据流提供书写基本类型及String的方法,而对象流可直接传输Java对象(其他流个人自行学习使用,在此重点以对象流展开测试)。
案例一(对象流的使用)
- User类
package com.hpr.entity;
import java.io.Serializable;
//想要在流之中传输,该对象类必须实现序列化接口
public class User implements Serializable {
private int id;
private String name;
private int age;
public User() {
}
public User(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
- 写入测试
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("./rootDir/oos.txt"));
//写入三个对象(写入时进行序列化)
oos.writeObject(new User(1001, "Alice", 18));
oos.writeObject(new User(1002, "Bob", 20));
oos.writeObject(new User(1003, "Clover", 22));
oos.flush();
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("程序执行完毕!");
执行结果
对象流一般在网络中传输,并不会以文件形式进行操作,在此为方便测试。
- 读取测试
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("./rootDir/oos.txt"));
//读取对象(读取时进行反序列化)
System.out.println(ois.readObject());
System.out.println(ois.readObject());
System.out.println(ois.readObject());
ois.close();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
执行结果
三、配置文件
- 后缀是ini、properties、xml的文件一般为配置文件,通常作为加载环境时所需的设置和文件集合;
- 格式:键=值(例如 id=1001,name=Alice, password=123456);
写入配置
try {
//创建配置文件
File file = new File("./rootDir/application.properties");
if (!file.exists()) {
file.createNewFile();
}
//加载配置文件
Properties p = new Properties();
p.load(new FileReader(file));
//设置参数
p.setProperty("id", "1001");
p.setProperty("name", "Alice");
p.setProperty("password", "123456");
//存储文件
p.store(new FileWriter(file), "hpr");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("程序执行结束!");
执行结果
读取配置
try {
//加载配置文件
Properties p = new Properties();
p.load(new FileReader("./rootDir/application.properties"));
//获取参数
System.out.println(p.get("id") + "——" + p.get("name") + "——" + p.get("password"));
} catch (IOException e) {
e.printStackTrace();
}
执行结果
总结
重点
- 转换流的作用及使用;
- 常用高层流;
- 配置文件的加载及使用。
难点
- 转换流逻辑及写法;
- 对象流序列化及反序列化。