第二十八章
一、缓冲流
1.1 缓冲流的原理
1.2 BufferedOutputStream–字节缓冲输出流
java.io.BufferedOutputStream extends OutputStream
BufferedOutputStream :字节缓冲输出流
继承自父类的共性成员方法:
void close() 关闭此输出流并释放与此流有关的所有系统资源。
void flush() 刷新此输出流,并强制将所有已缓冲的输出字节写入该流中。
void write(byte[] b) 将 b.length 个字节写入此输出流。
void write(byte[] b, int off, int len) 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。
void write(int b) 将指定 byte 写入此输出流。
构造方法:
BufferedOutputStream(OutputStream out)
创建一个新的缓冲输出流,以将数据写入指定的底层输出流。
BufferedOutputStream(OutputStream out, int size)
创建一个新的缓冲输出流,以将具有指定缓冲区大小的数据写入指定的底层输出流。
参数: OutputStream out :字节输出流
我们可以传递FileOutputStream,缓冲流会给FileOutputStream增加一个缓冲区,提高FileOutputStream的写入效率
int size :指定缓冲流内部缓冲区的大小,不指定,默认大小
使用步骤:
1.创建FileOutputStream对象,绑定要输出的目的地
2.创建 BufferedOutputStream对象,构造方法中传递FileOutputStream,提高FileOutputStream对象效率
3.使用 BufferedOutputStream对象中的方法write,把数据写入到内部缓冲区中
4.使用 BufferedOutputStream对象中的方法flush,把内部缓冲区中的数据刷新到文件当中
5.释放资源(会先调用flush方法刷新数据)
public class Demo01BufferedOutputStream {
public static void main(String[] args) throws IOException {
// 1.创建FileOutputStream对象,绑定要输出的目的地
FileOutputStream fos = new FileOutputStream("Day17-code\\a.txt");
//2.创建 BufferedOutputStream对象,构造方法中传递FileOutputStream,提高FileOutputStream对象效率
BufferedOutputStream bos = new BufferedOutputStream(fos);
// 3.使用 BufferedOutputStream对象中的方法write,把数据写入到内部缓冲区中
bos.write("我们把数据写入到内部缓冲区中".getBytes());//这时创建了文件但内容并没有传到文件中
// 4.使用 BufferedOutputStream对象中的方法flush,把内部缓冲区中的数据刷新到文件当中
bos.flush();//内容传递到文件中
//5.释放资源(会先调用flush方法刷新数据)
bos.close();
}
}
1.3 BufferedInputStream–字节缓冲输入流
java.io.BufferedInputStream extends InputStream
BufferedInputStream :字节缓冲输入流
继承自父类的共性成员方法:
int read() 从此输入流中读取一个数据字节。
int read(byte[] b) 从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。
void close() 关闭此文件输入流并释放与此流有关的所有系统资源。
构造方法:
BufferedInputStream(InputStream in)
创建一个 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。
BufferedInputStream(InputStream in, int size)
创建具有[指定缓冲区大小]的 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。
参数: InputStream in :字节输入流
我们可以传递FileInputStream,缓冲流会给FileInputStream增加一个缓冲区,提高FileInputStream的读取效率
int size :指定缓冲流内部缓冲区的大小,不指定,默认大小
使用步骤(重点):
1.创建FileInputStream对象,绑定要读取的数据源
2.创建 BufferedInputStream对象,构造方法中传递FileInputStream,提高FileInputStream读取效率
3.使用 BufferedInputStream对象中的方法read,读取文件
4.释放资源(会先调用flush方法刷新数据)
public class Demo02BufferedInputStream {
public static void main(String[] args) throws IOException {
// 1.创建FileInputStream对象,绑定要读取的数据源
FileInputStream fis = new FileInputStream("Day17-code\\a.txt");
//2.创建 BufferedInputStream对象,构造方法中传递FileInputStream,提高FileInputStream读取效率
BufferedInputStream bis = new BufferedInputStream(fis);
// 3.使用 BufferedInputStream对象中的方法read,读取文件
/* int len = 0;//记录每次读取到的字节
while ((len =bis.read())!=-1){
System.out.println(len);
}*/
// int read(byte[] b) 从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。
byte[] bytes = new byte[1024];
int len = 0;//记录有效的字节数 read 要有数组参数
while ((len = bis.read(bytes))!= -1){
System.out.println(new String(bytes,0,len));
}
//4.释放资源 关闭缓冲流,自动关闭字节流
bis.close();
}
}
1.4 缓冲的效率测试–复制文件
/*
文件复制步骤:
1.创建字节缓冲输入流对象,构造方法中传递字节输入流
2.创建字节缓冲输出流对象,构造方法中传递字节输出流
3.使用字节缓冲输入流对象中的方法read,读取文件
4.使用字节缓冲输出流中的方法write,把读取的数据写入到内部缓冲区
5.释放资源(会先把缓冲区中的数据刷新到文件中)
一次读取一个字节:复制文件共耗时22毫秒
一次读取多个字节:复制文件共耗时2毫秒
*/
public class BufferedFileCopy {
public static void main(String[] args) throws IOException {
long s = System.currentTimeMillis();
// 1.创建字节缓冲输入流对象,构造方法中传递字节输入流
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("Day17-code\\111.JPG"));
// 2.创建字节缓冲输出流对象,构造方法中传递字节输出流
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("f:\\111.JPG"));
//3.使用字节缓冲输入流对象中的方法read,读取文件
//一次读取一个字节
/* int len = 0;
while ((len = bis.read())!= -1){
bos.write(len);
}*/
//使用数组缓冲读取多个字节,写入多个字节
byte[] bytes = new byte[1024];
int len =0;
while ((len = bis.read(bytes))!= -1){
bos.write(bytes,0,len);
}
bos.close();
bis.close();
long e = System.currentTimeMillis();
System.out.println("复制文件共耗时"+(e-s)+"毫秒");
}
}
1.5 BufferedWriter-字符缓冲输出流
java.io.BufferedWriter extends writer BufferedWriter:字符缓冲输出流
继承自父类的共性成员方法:
abstract void close() 关闭此流,但要先刷新它。
abstract void flush() 刷新该流的缓冲。
void write(char[] cbuf) 写入字符数组。
abstract void write(char[] cbuf, int off, int len) 写入字符数组的某一部分。
void write(int c) 写入单个字符。
void write(String str) 写入字符串。
void write(String str, int off, int len) 写入字符串的某一部分。
构造方法:
BufferedWriter(Writer out)
创建一个使用默认大小输出缓冲区的缓冲字符输出流。
BufferedWriter(Writer out, int sz)
创建一个使用给定大小输出缓冲区的新缓冲字符输出流。
参数:
Writer out :字符输出流
我们可以传递FileWriter,缓冲流会给FileWriter增加一个缓冲区,提高FileWriter的写入效率
int sz: 指定缓冲区的大小
特有的成员方法:
void newLine() 写入一个行分隔符。
会根据不同的系统写入一个不同的行分隔符
使用步骤:
1.创建字符缓冲输出流对象,构造方法中传递字符输出流
2.调用字符缓冲输出流中的方法write,把数据写入到内存缓冲区中。
3.调用字符缓冲输出流中的方法flush,把数据刷新到文件中
4.释放资源
public class Demo01BufferedWriter {
public static void main(String[] args) throws IOException {
// System.out.println(); 调用的输出语句就是new line()
// 1.创建字符缓冲输出流对象,构造方法中传递字符输出流
BufferedWriter bw = new BufferedWriter(new FileWriter("Day17-code\\b.txt"));
//2.调用字符缓冲输出流中的方法write,把数据写入到内存缓冲区中。
for (int i = 0; i < 10; i++) {
bw.write("程序员");
// bw.write("\r\n");
bw.newLine();
}
// 3.调用字符缓冲输出流中的方法flush,把数据刷新到文件中
bw.flush();
//4.释放资源
bw.close();
}
}
1.6 BufferedReader-字符缓冲输入流
int read() 读取单个字符。
int read(char[] cbuf) 将字符读入数组。
abstract void close() 关闭该流并释放与之关联的所有资源
构造方法:
BufferedReader(Reader in) 创建一个使用默认大小输入缓冲区的缓冲字符输入流。
BufferedReader(Reader in, int sz) 创建一个使用指定大小输入缓冲区的缓冲字符输入流。
参数:
Reader in:字符输入流
我们可以传递FileReader,缓冲流会给FileReader增加一个缓冲区,提高FileReader的读取效率
int sz: 大小
特有的成员方法: String readLine() 读取一个文本行。
读取一个文本行。通过下列字符之一即可认为某行已终止:
换行 ('\n')、回车 ('\r') 或回车后直接跟着换行。
返回值:包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null
使用步骤:
1.创建一个字符缓冲输入流对象,构造方法中传递字符输入流
2.使用字符缓冲输入流对象中的方法 read/readLine 读取文本
3.释放资源
/*
*/
public class Demo02BufferedReader {
public static void main(String[] args) throws IOException {
// 1.创建一个字符缓冲输入流对象,构造方法中传递字符输入流
BufferedReader br = new BufferedReader(new FileReader("Day17-code\\b.txt"));
// 2.使用字符缓冲输入流对象中的方法 read/readLine 读取文本
/* String line = br.readLine();//只能读取一行
System.out.println(line);*/
//优化readLine 读取多行 while循环 读到null结束
/* String line;
while ((line = br.readLine())!= null){
System.out.println(line);
}*/
int len = 0;
while ((len = br.read())!= -1){
System.out.println((char)len);
}
// 3.释放资源
br.close();
}
}
1.7 练习:对文本内容进行排序
分析:
1.创建一个HashMap集合对象,可以:存储每行文本的序号(1,2,3...)value:存储每行的文本
2.创建字符缓冲输入流对象,构造方法中绑定字符输入流
3.创建字符缓冲输出流对象,构造方法中绑定字符输出流
4.使用字符缓冲输入流中的方法readline,逐行读取文本
5.对读取到的文本进行切割,获取行中的序号和文本内容
6.把切割好的序号和文本的内容存储到HashMap集合中(key序号是有序的,会自动排序1,2,3,4)
7.遍历HashMap集合,获取每一个键值对
8.把每一个键值对,拼接为一个文本行
9.把拼接好的文本,使用字符缓冲输出流中的方法write,写入到文件中
10.释放资源
public class Demo03test {
public static void main(String[] args) throws IOException {
// 1.创建一个HashMap集合对象,可以:存储每行文本的序号(1,2,3...)value:存储每行的文本
HashMap<String,String> map =new HashMap<>();
// 2.创建字符缓冲输入流对象,构造方法中绑定字符输入流
BufferedReader br = new BufferedReader(new FileReader("Day17-code//c.txt"));
// 3.创建字符缓冲输出流对象,构造方法中绑定字符输出流
BufferedWriter bw = new BufferedWriter(new FileWriter("Day17-code//out.txt"));
// 4.使用字符缓冲输入流中的方法readline,逐行读取文本
String line ;
while ((line = br.readLine())!=null){
// 5.对读取到的文本进行切割,获取行中的序号和文本内容
String[] arr = line.split("\\.");
// 6.把切割好的序号和文本的内容存储到HashMap集合中(key序号是有序的,会自动排序1,2,3,4)
map.put(arr[0],arr[1]);
}
// 7.遍历HashMap集合,获取每一个键值对
for (String key: map.keySet()
) {
String value = map.get(key);
// 8.把每一个键值对,拼接为一个文本行
line = key +"." +value;
// 9.把拼接好的文本,使用字符缓冲输出流中的方法write,写入到文件中
bw.write(line);
bw.newLine();
}
//10.释放资源
bw.close();
br.close();
}
}
二、 转换流
2.1 字符编码和字符集
字符编码Character Encoding:就是一套自然语言的字符与二进制数之间的对应规则。
编码:字符–>字节
解码:字节(看不懂的)–>字符(能看懂的)
字符编码CharSet: 也叫编码表,是一个系统支持的所有字符的集合。(生活中文字和计算机中二进制的对应规则)
2.2 编码引出的问题(FileReader读取GBK格式的文件)
/*
FileReader 可以读取默认编码格式(UTF-8)
FileReader读取系统默认编码(中文GBK)会产生乱码���
*/
public class Demo01FileReader {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("Day17-code\\我是GBK格式的文档.txt");
int len = 0 ;
while((len = fr.read())!= -1){
System.out.print((char) len);
}
}
}
2.3 转换流的原理
2.4 OutputStreamWriter介绍&代码实现
java.io.OutputStreamWriter extends Writer
OutputStreamWriter:是字符流通向字节流的桥梁:可使用指定的charset将要写入流的字符编码成字节
继承自父类的共性成员方法:
abstract void close()
关闭此流,但要先刷新它。
abstract void flush()
刷新该流的缓冲。
void write(char[] cbuf)
写入字符数组。
abstract void write(char[] cbuf, int off, int len)
写入字符数组的某一部分。
void write(int c)
写入单个字符。
void write(String str)
写入字符串。
void write(String str, int off, int len)
写入字符串的某一部分。
构造方法:
OutputStreamWriter(OutputStream out) 创建使用默认字符编码的 OutputStreamWriter。
OutputStreamWriter(OutputStream out, String charsetName) 创建 使用指定字符集的 OutputStreamWriter。
参数:
OutputStream out: 字节输出流,可以用来写转换之后的字节到文件中
String charsetName: 指定编码表的名称。不区分到小写 utf-8/GBK。。。
不指定默认使用utf-8格式
使用步骤:
1.创建OutputStreamWriter对象,构造方法中传递字节输出流和指定的编码表名称
2.使用OutputStreamWriter对象中的方法writer,把字符转换为字节存储缓冲区中(编码)
3.使用OutputStreamWriter对象中的方法flush,把内存缓冲区中的字节刷新到文件中(使用字节流写字节的过程)
4.释放资源
public class Demo02OutputStreamWriter {
public static void main(String[] args) throws IOException {
//write_utf_8();//6个字节
write_GBK_8();//4个字节
}
/*
使用转换流OutputStreamWriter写GBK格式的文件
*/
private static void write_GBK_8() throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("Day17-code\\gbk.txt"),"gbk");
osw.write("你好");//4个字节
osw.flush();
osw.close();
}
/*
使用转换流OutputStreamWriter写UTF-8格式的文件
*/
private static void write_utf_8() throws IOException {
// 1.创建OutputStreamWriter对象,构造方法中传递字节输出流和指定的编码表名称
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("Day17-code\\utf_8.txt"),"utf-8");
// 2.使用OutputStreamWriter对象中的方法writer,把字符转换为字节存储缓冲区中(编码)
osw.write("你好");
// 3.使用OutputStreamWriter对象中的方法flush,把内存缓冲区中的字节刷新到文件中(使用字节流写字节的过程)
osw.flush();
//4.释放资源
osw.close();
}
}
2.5 InputStreamReader介绍&代码实现
java.io.InputStreamReader extends Reader:它使用指定的 charset 读取字节并将其解码为字符
继承的方法:
int read() 从此输入流中读取一个数据字节。
int read(byte[] b) 从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。
void close() 关闭此文件输入流并释放与此流有关的所有系统资源。
构造方法:
InputStreamReader(InputStream in)
创建一个使用默认字符集的 InputStreamReader。
InputStreamReader(InputStream in, String charsetName)
创建使用指定字符集的 InputStreamReader。
参数:InputStream in:字节输入流,用来读取文件中保存的字节
charsetName:指定的编码表名称,不区分大小写
使用步骤:
1.创建InputStreamReader对象,构造方法中传递字节输入流和指 定的编码表名称
2.使用InputStreamReader对象中的方法read读取文件
3.释放资源
public class Demo03InputStreamReader {
public static void main(String[] args) throws IOException {
// read_utf_8();
read_GBK();
}
/*
读取GBK格式的文件
*/
private static void read_GBK() throws IOException {
// 1.创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称
InputStreamReader isr = new InputStreamReader(new FileInputStream("Day17-code\\gbk.txt"),"gbk");
// 2.使用InputStreamReader对象中的方法read读取文件
int len =0;
while((len =isr.read())!= -1){
System.out.print((char) len);
}
//3.释放资源
isr.close();
}
/*
读取utf-8格式的文件
*/
private static void read_utf_8() throws IOException {
// 1.创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称
InputStreamReader isr = new InputStreamReader(new FileInputStream("Day17-code\\utf_8.txt"));
// 2.使用InputStreamReader对象中的方法read读取文件
int len =0;
while((len =isr.read())!= -1){
System.out.print((char) len);
}
//3.释放资源
isr.close();
}
}
2.6 练习:转换文件编码
/*
分析
1.创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称GBK
2.创建OutputStreamWriter对象,构造方法中传递字节输出流和指定的编码表名称UTF-8
3.使用InputStreamReader对象的read方法读取文件
4.使用OutputStreamWriter对象中的方法write,把读取到数据写入到文件中
5.释放资源
*/
public class Demo04Transform {
public static void main(String[] args) throws IOException {
//1
InputStreamReader isr = new InputStreamReader(new FileInputStream("Day17-code\\我是GBK格式的文档.txt"),"gbk");
//2
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("Day17-code\\我是utf_8.txt"),"utf-8");
//3
int len = 0;
while ((len = isr.read())!= -1){
//4
osw.write(len);
}
//5.
isr.close();
osw.close();
}
}
三、序列化
3.1 概述
序列化:把Java对象转换为字节序列。
反序列化:把字节序列恢复为原先的Java对象。
3.2 对象的序列化流—ObjectOutputStream
java.io.ObjectOutputStream extends OutputStream
ObjectOutputStream :对象的序列化流
作用:把对象以流的方法写入到文件中保存
构造方法:
ObjectOutputStream(OutputStream out) 创建写入指定 OutputStream 的 ObjectOutputStream。
特有的成员方法:
void writeObject(Object obj) 将指定的对象写入 ObjectOutputStream。
使用步骤:
1.传概念ObjectOutputStream对象,构造方法中传递字节输出流
2.使用ObjectOutputStream对象中的方法writeObject,把对象写入到文件中
3.释放资源
NotSerializableException:未序列化异常
public class Demo01ObjectOutputStream {
public static void main(String[] args) throws IOException {
// 1.传概念ObjectOutputStream对象,构造方法中传递字节输出流
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Day17-code\\person.txt"));
// 2.使用ObjectOutputStream对象中的方法writeObject,把对象写入到文件中
oos.writeObject(new Person("小美女",18));//用字节存储的,无法直接打开文件,会乱码
// 3.释放资源
oos.close();
}
}
3.2 对象的反序列化流—ObjectInputStream
java.io.ObjectInputStream extends InputStream
ObjectInputStream :对象的反序列化流
作用:把文件中保存的对象,以流的方式读出来
构造方法:
ObjectInputStream(InputStream in) 创建写入指定 InputStream 读取的 ObjectInputStream。
参数:InputStream in :字节输入流
特有的成员方法:
Object readObject(Object obj) 从ObjectInputStream读取对象
使用步骤:
1.创建ObjectInputStream对象,构造方法中传递字节输入流
2.使用ObjectInputStream对象中的方法readeObject,读取保存对象的文件
3.释放资源
4.使用读取出来的对象()打印
NotSerializableException:未序列化异常
readObject()方法:声明抛出了ClassNotFoundException(class文件找不到异常)
当不存在对象的class文件时抛出此异常:
1.类必须实现Serializable
2.必须存在类对应的class文件
public class Demo02ObjectInputStream {
public static void main(String[] args) throws IOException, ClassNotFoundException {
// 1.创建ObjectInputStream对象,构造方法中传递字节输入流
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Day17-code\\person.txt"));
//2.使用ObjectInputStream对象中的方法readeObject,读取保存对象的文件
Object object = ois.readObject();
//3.释放资源
ois.close();
//4.使用读取出来的对象()打印
System.out.println(object);//Person{name='小美女', age=18}
//强转为person类型
Person p = (Person)object;
System.out.println(p.getName()+p.getAge());
}
}
3.3 transient关键字—瞬态关键字
static 关键字:静态关键字
静态优先于非静态加载到内存中(静态优先于对象进入到内存中)
被static修饰的成员变量不能被序列化的,序列化的都是对象
transient关键字:瞬态关键字(如果不想变量被序列化就用transient)
被transient修饰的成员变量不能被序列化的.
3.4 InvalidClassException异常—原理和解决方案
序列化运行时使用一个称为 serialVersionUID 的版本号与每个可序列化类相关联,该序列号在反序列化过程中用于验证序列化对象的发送者和接收者是否为该对象加载了与序列化兼容的类。如果接收者加载的该对象的类的 serialVersionUID 与对应的发送者的类的版本号不同,则反序列化将会导致 InvalidClassException。可序列化类可以通过声明名为 "serialVersionUID" 的字段(该字段必须是静态 (static)、最终 (final) 的 long 型字段)显式声明其自己的 serialVersionUID:
在对应的类中声明:
static final long serialVersionUID = 42L;
3.5 序列化集合
练习:序列化集合
当我们想在文件中保存多个对象的时候
可以把多个对象存储到一个集合中
对集合进行序列化和反序列化
分析:
1.定义一个存储Person对象的ArrayList集合
2.往ArrayList集合中存储Person对象
3.创建一个序列化流ObjectOutputStream对象
4.使用ObjectOutputStream对象中的方法writeObject,对集合进行序列化
5.创建一个反序列化ObjectInputStream对象
6.使用ObjectInputStream对象中的方法readObject读取文件中保存的集合
7.把Object类型的集合转换为ArrayList类型
8.遍历ArrayList集合
9.释放资源
public class Demo03Test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//1.定义一个存储Person对象的ArrayList集合
ArrayList<Person> list= new ArrayList<>();
// 2.往ArrayList集合中存储Person对象
list.add(new Person("张三",18));
list.add(new Person("李四",19));
list.add(new Person("王五",20));
// 3.创建一个序列化流ObjectOutputStream对象
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Day17-code\\list.txt"));
//4.使用ObjectOutputStream对象中的方法writeObject,对集合进行序列化
oos.writeObject(list);
//5.创建一个反序列化ObjectInputStream对象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Day17-code\\list.txt"));
//6.使用ObjectInputStream对象中的方法readObject读取文件中保存的集合
Object o = ois.readObject();
//7.把Object类型的集合转换为ArrayList类型
ArrayList<Person> list2 = (ArrayList<Person>) o;
//8.遍历ArrayList集合
for (Person person : list2) {
System.out.println(person);
}
// 9.释放资源
ois.close();
oos.close();
}
}
3.6 打印流-概述和使用
/*
可以改变输出语句的目的地(打印的流向)
输出语句,默认在控制台输出
使用System.setOut方法改变输出语句的目的地,改为参数中传递的打印流的目的地
static void setOut(PrintStream out)
重新分配“标准”输出流
*/
public class Demo05PrintStream {
public static void main(String[] args) throws FileNotFoundException {
System.out.println("我是在控制台输出的");
PrintStream ps = new PrintStream("Day17-code\\print.txt");
System.setOut(ps);//把输出语句的目的地改为打印流的目的地
System.out.println("我在打印流的目的地中输出");
System.out.println("AA");
ps.close();
}
}