目录
处理流-BufferedInputStream和BufferedOutputStream
在Java中,BufferedInputStream和BufferedOutputStream是两个非常重要的类,它们的主要作用是提高I/O性能。接下来看看
BufferedInputStream类是为了提高读取效率而设计的。它在内部维护了一个缓冲区,每次从输入流中读取数据时,它会将数据读入到缓冲区中。然后,它将返回缓冲区中的数据而不是每次直接从输入流中读取数据。这可以大大减少访问底层输入流的次数,从而提高读取效率。
BufferedOutputStream类也是为了提高写入效率而设计的。它在内部维护了一个缓冲区,每次进行写入操作时,它会将数据先写入到缓冲区中,然后再一次性将缓冲区中的数据写出到底层输出流中。这样可以减少写入操作对底层输出流的访问次数,从而提高写入效率。
相比于直接使用底层输入输出流,使用BufferedInputStream和BufferedOutputStream类能够提高I/O操作的效率和性能。
代码演示:
和之前的读取文件一样,定义要读取文件的路径和写入到哪个文件,这里我们拷贝的是二进制文件,也就是音频等文件
- package com.outputstream_;
-
- import com.reader_.BufferedReader_;
- import com.sun.xml.internal.org.jvnet.mimepull.MIMEConfig;
-
- import java.io.*;
-
- /**
- * 演示使用BufferedOutputStream 和 BufferedInputStream使用
- * 使用他们,可以完成二进制文件拷贝.
- * 思考:字节流可以操作二进制文件,可以操作文本文件吗?当然可以
- */
- public class BufferedCopy02 {
- public static void main(String[] args) {
-
- // String srcFilePath = "e:\\Koala.jpg";
- String srcFilePath = "e:\\asd.java";
- String destFilePath = "e:\\a1.java";
-
- //创建BufferedOutputStream对象BufferedInputStream对象
- BufferedInputStream bis = null;
- BufferedOutputStream bos = null;
-
- try {
- //因为 FileInputStream 是 InputStream 子类
- bis = new BufferedInputStream(new FileInputStream(srcFilePath));
- bos = new BufferedOutputStream(new FileOutputStream(destFilePath));
-
- //循环的读取文件,并写入到 destFilePath
- byte[] buff = new byte[1024];
- int readLen = 0;
- //当返回 -1 时,就表示文件读取完毕
- while ((readLen = bis.read(buff)) != -1) {
- bos.write(buff, 0, readLen);
- }
-
- System.out.println("文件拷贝完毕~~~");
-
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
-
- //关闭流 , 关闭外层的处理流即可,底层会去关闭节点流
- try {
- if (bis != null) {
- bis.close();
- }
- if (bos != null) {
- bos.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- }
- }
- }
对象流-ObjectInputStream和ObjectOutputStream
1.序列化就是在保存数据时,保存数据的值和数据类型
2.反序列化就是在恢复数据时,恢复数据的值和数据类型
3.需要让某个对象支持序列化机制,则必须让其类是可序列化的,为了让某个类是可序列化的,该类必须实现如下两个接口之
-Serializable// 这是一个标记接口,没有方法
Externalizable // 该接口有方法需要实现,因此我们一般实现上面的 Serializable接口
1.功能:提供了对基本类型或对象类型的序列化和反序列化的方法
2.ObjectOutputStream 提供 序列化功能
3.ObjectInputStream 提供 反序列化功能
完成文件的序列化
代码演示:
如果需要序列化某个类的对象,实现 Serializable接口,在序列化对象时,默认将里面所有属性都进行序列化,但除了static或transient修饰的成员,序列化对象时,要求里面属性的类型也需要实现序列化接口,然后使用ObjectOutputStream序列化文件
- package com.outputstream_;
-
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.ObjectOutputStream;
-
- /**
- * 演示ObjectOutputStream的使用, 完成数据的序列化
- */
- public class ObjectOutStream_ {
- public static void main(String[] args) throws Exception {
- //序列化后,保存的文件格式,不是存文本,而是按照他的格式来保存
- String filePath = "e:\\data.dat";
-
- ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath));
-
- //序列化数据到 e:\data.dat
- oos.writeInt(100);// int -> Integer (实现了 Serializable)
- oos.writeBoolean(true);// boolean -> Boolean (实现了 Serializable)
- oos.writeChar('a');// char -> Character (实现了 Serializable)
- oos.writeDouble(9.5);// double -> Double (实现了 Serializable)
- oos.writeUTF("你好");//String
- //保存一个dog对象
- oos.writeObject(new Dog("旺财", 10, "日本", "白色"));
- oos.close();
- System.out.println("数据保存完毕(序列化形式)");
-
-
- }
- }
-
- package com.outputstream_;
-
- import java.io.Serializable;
-
- //如果需要序列化某个类的对象,实现 Serializable
- public class Dog implements Serializable {
- private String name;
- private int age;
- //序列化对象时,默认将里面所有属性都进行序列化,但除了static或transient修饰的成员
- private static String nation;
- private transient String color;
- //序列化对象时,要求里面属性的类型也需要实现序列化接口
- private Master master = new Master();
-
- //serialVersionUID 序列化的版本号,可以提高兼容性
- private static final long serialVersionUID = 1L;
-
- public Dog(String name, int age, String nation, String color) {
- this.name = name;
- this.age = age;
- this.color = color;
- this.nation = nation;
- }
-
- @Override
- public String toString() {
- return "Dog{" +
- "name='" + name + '\'' +
- ", age=" + age +
- ", color='" + color + '\'' +
- '}' + nation + " " +master;
- }
-
- 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;
- }
- }
反序列化
代码演示:
在反序列化的时候,要注意,反序列化的时候,读取(反序列化)的顺序需要和你保存数据(序列化)的顺序一致,否则会有异常
- package com.inputstream_;
-
- import com.outputstream_.Dog;
-
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
-
- public class ObjectInputStream_ {
- public static void main(String[] args) throws IOException, ClassNotFoundException {
-
- //指定反序列化的文件
- String filePath = "e:\\data.dat";
-
- ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath));
-
- //读取
- //解读
- //1. 读取(反序列化)的顺序需要和你保存数据(序列化)的顺序一致
- //2. 否则会出现异常
-
- 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 的编译类型是 Object , dog 的运行类型是 Dog
- Object dog = ois.readObject();
- System.out.println("运行类型=" + dog.getClass());
- System.out.println("dog信息=" + dog);//底层 Object -> Dog
-
- //这里是特别重要的细节:
-
- //1. 如果我们希望调用Dog的方法, 需要向下转型
- //2. 需要我们将Dog类的定义,放在到可以引用的位置
- Dog dog2 = (Dog)dog;
- System.out.println(dog2.getName()); //旺财..
-
- //关闭流, 关闭外层流即可,底层会关闭 FileInputStream 流
- ois.close();
-
-
- }
- }
-
- package com.outputstream_;
-
- import java.io.Serializable;
-
- //如果需要序列化某个类的对象,实现 Serializable
- public class Dog implements Serializable {
- private String name;
- private int age;
- //序列化对象时,默认将里面所有属性都进行序列化,但除了static或transient修饰的成员
- private static String nation;
- private transient String color;
- //序列化对象时,要求里面属性的类型也需要实现序列化接口
- private Master master = new Master();
-
- //serialVersionUID 序列化的版本号,可以提高兼容性
- private static final long serialVersionUID = 1L;
-
- public Dog(String name, int age, String nation, String color) {
- this.name = name;
- this.age = age;
- this.color = color;
- this.nation = nation;
- }
-
- @Override
- public String toString() {
- return "Dog{" +
- "name='" + name + '\'' +
- ", age=" + age +
- ", color='" + color + '\'' +
- '}' + nation + " " +master;
- }
-
- 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;
- }
- }
1)读写顺序要一致
2)要求序列化或反序列化对象,需要 实现 Serializable
3序列化的类中建议添加SerialVersionUID,为了提高版本的兼容性
4)序列化对象时,默认将里面所有属性都进行序列化,但除了static或transient修饰的成员
5)序列化对象时,要求里面属性的类型也需要实现序列化接口
6)序列化具备可继承性,也就是如果某类已经实现了序列化,则它的所有子类也已经默认实现了序列化
代码演示:
- package com.standard;
-
- import java.util.Scanner;
-
- public class InputAndOutput {
- public static void main(String[] args) {
- //System 类 的 public final static InputStream in = null;
- // System.in 编译类型 InputStream
- // System.in 运行类型 BufferedInputStream
- // 表示的是标准输入 键盘
- System.out.println(System.in.getClass());
-
- //解读
- //1. System.out public final static PrintStream out = null;
- //2. 编译类型 PrintStream
- //3. 运行类型 PrintStream
- //4. 表示标准输出 显示器
- System.out.println(System.out.getClass());
-
- System.out.println("hello, 你好~");
-
- Scanner scanner = new Scanner(System.in);
- System.out.println("输入内容");
- String next = scanner.next();
- System.out.println("next=" + next);
-
-
- }
- }
首先我们来看看为什么会出现乱码问题
那么乱码可能是以下原因导致的
编码不匹配:转换流可以将字节流转换成字符流,如果在转换的过程中使用了错误的编码方式,就会出现乱码问题。
字符集不支持:有些字符集在Java中不被支持,如果在转换流中使用了不支持的字符集,也会出现乱码问题。
字节流长度不匹配:如果转换流在读取字节流时,读取到的字节长度超出了字符流的长度限制,就会出现乱码问题。
解决方法
代码演示
- package com.transformation;
-
- import java.io.*;
-
- /**
- * 演示使用 InputStreamReader 转换流解决中文乱码问题
- * 将字节流 FileInputStream 转成字符流 InputStreamReader, 指定编码 gbk/utf-8
- */
- public class InputStreamReader_ {
- public static void main(String[] args) throws IOException {
-
- String filePath = "e:\\abc.txt";
- //解读
- //1. 把 FileInputStream 转成 InputStreamReader
- //2. 指定编码 gbk
- //InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath), "gbk");
- //3. 把 InputStreamReader 传入 BufferedReader
- //BufferedReader br = new BufferedReader(isr);
-
- //将2 和 3 合在一起
- BufferedReader br = new BufferedReader(new InputStreamReader(
- new FileInputStream(filePath), "gbk"));
-
- //4. 读取
- String s = br.readLine();
- System.out.println("读取内容=" + s);
- //5. 关闭外层流
- br.close();
-
- }
- }