IO流-缓冲流

缓冲流、转换流、序列化流

缓冲流,也叫高效流,是对4个基本流的增强,所以也是4个流。按照数据类型分类:
字节缓冲流: BufferedInputStream ,BufferedOutputStream
字符缓冲流: BufferedReader ,BufferedWriter
缓冲流的基本原理,是在创建流对象时,会创建一个内置的默认大小的缓冲区数组,通过缓冲区读写,减少系统IO 次数,从而提高读写的效率。

字节缓冲流的构造方法
public BufferedInputStream(InputStream in) :创建一个 新的缓冲输入流。
public BufferedOutputStream(OutputStream out) : 创建一个新的缓冲输出流。

字符缓冲流 构造方法
public BufferedReader(Reader in) :创建一个 新的缓冲输入流。
public BufferedWriter(Writer out) : 创建一个新的缓冲输出流。
特有方法 字符缓冲流的基本方法与普通字符流调用方式一致。
特有方法:
BufferedReader: public String readLine() : 读一行文字。
BufferedWriter: public void newLine() : 写一行行分隔符,由系统属性定义符号。

普通流与高效流的比较:
package Test;

/字符流,字节流,高效字符流,高效字节流/

import java.io.*;

public class Test {
    public static void main(String[] args) throws IOException {
        System.out.println("字节流:"+"-->"+method1());
        System.out.println("字节流-byte 数组:"+"-->"+method11());
        System.out.println("字符流--图片打不来了。。"+"-->"+method2());
        System.out.println("高效字符字节答应流 没有换行"+"-->"+method3());
        System.out.println("高效字符流  调用普通Write方法"+"-->"+method4());
        System.out.println("高效字符流,调用特有的WriteLine()方法"+"-->"+method5());
        //System.out.println(method6());

    }

    //字节流
    public static long method1() throws IOException {
        long a = System.currentTimeMillis();
        FileInputStream fis = new FileInputStream("D:\\a.jpg");
        FileOutputStream fos = new FileOutputStream("D:\\b.jpg");

        int len;
        while ((len = fis.read()) !=  -1) {
            fos.write(len);
        }
        long b = System.currentTimeMillis();
        fis.close();
        return b - a;
    }

    //字节流-byte 数组
    public static long method11() throws IOException {
        long a = System.currentTimeMillis();
        FileInputStream fis = new FileInputStream("D:\\a.jpg");
        FileOutputStream fos = new FileOutputStream("D:\\b.jpg");

        byte [] bytes=new byte[8*1024];
        int len;
        while ((len = fis.read(bytes)) != -1) {
            fos.write(bytes,0,len);
        }
        long b = System.currentTimeMillis();
        fis.close();
        return b - a;
    }

    //字符流--图片打不来了。。
    public static long method2() throws IOException {
        long a = System.currentTimeMillis();
        FileReader fr = new FileReader("D:\\b.jpg");
        FileWriter fw = new FileWriter("D:\\C.jpg");
        int len;
        while ((len = fr.read()) != -1) {
            fw.write(len);
        }
        long b = System.currentTimeMillis();
        fr.close();
        fw.close();
        return b - a;

    }

    //高效字符字节答应流 没有换行
    public static long method3() throws IOException {
        long a = System.currentTimeMillis();
        BufferedInputStream bfs = new BufferedInputStream(new FileInputStream("D:\\a.jpg"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\D.jpg"));
        int len;
        while ((len = bfs.read()) != -1) {
            bos.write(len);
        }
        bos.write(len);
        long b = System.currentTimeMillis();
        return b - a;

    }

    //高效字符流  调用普通Write方法
    public static long method4() throws IOException {
        long a = System.currentTimeMillis();
        BufferedReader br = new BufferedReader(new FileReader("D:\\a.jpg"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\e.jpg"));
        int len;
        while ((len = br.read()) != -1) {
            bw.write(len);
        }
        bw.write(len);
        long b = System.currentTimeMillis();
        return b - a;

    }

    //高效字符流,调用特有的WriteLine()方法
    public static long method5() throws IOException {
        long a = System.currentTimeMillis();
        BufferedReader br = new BufferedReader(new FileReader("D:\\a.jpg"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\e.jpg"));
        String len;
        while ((len = br.readLine()) != null) {
            bw.write(len);
            bw.newLine();
        }
        bw.close();
        long b = System.currentTimeMillis();
        return b - a;

    }

  /*  //字节转换流 图片显示不了不知道编码格式
    public static long method6() throws IOException {
        long a = System.currentTimeMillis();
        InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\\a.jpg"));
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\f.jpg"));

        int len;
        while ((len = isr.read()) != -1) {
            osw.write(len);
        }
        isr.close();
        osw.close();

        long b = System.currentTimeMillis();
        return b - a;

    }*/

}

结果
这里写图片描述
转换流-字符编码和字符集
字符编码 计算机中储存的信息都是用二进制数表示的。按照某种规则,将字符存储到计算机中,称为编码 。反之,将存储在计算机中的二进制数按照 某种规则解析显示出来,称为解码 。
字符集 Charset :也叫编码表。是一个系统支持的所有字符的集合,包括各国家文字、标点符号、图形符 号、数字等。常见字符 集有ASCII字符集、GBK字符集、Unicode字符集等。

ASCII字符集 :ASCII是基于拉丁字母的一套电脑编码系统,用于显示现代英语,主要包括控制字符(回车键、退格、换行键等)和可显示字符(英文大小写字符、阿拉伯数字和西文符号)。 基本的ASCII字符集,使用7位(bits)表示一个字符,共128字符。ASCII的扩展字符集使用8位(bits) 表示一个字符,共256字符,方便支持欧洲常用字符。
GBxxx字符集: GB就是国标的意思,是为了显示中文而设计的一套字符集。 GB2312:简体中文码表。一个小于127的字符的意义与原来相同。但两个大于127的字符连在一起时, 就表示一个汉字。
GBK:最常用的中文码表。是在GB2312标准基础上的扩展规范,使用了双字节编码方案。
Unicode字符集 : Unicode编码系统为表达任意语言的任意字符而设计,是业界的一种标准,也称为统一码、标准万国码。 它最多使用4个字节的数字来表达每个字母、符号,或者文字。有三种编码方案,UTF-8、UTF-16和UTF32。最为常用的UTF-8编码。

InputStreamReader与OutputStreamWriter类
InputStreamReader类
转换流 java.io.InputStreamReader ,是Reader的子类,是从字节流到字符流的桥梁。
构造方法
InputStreamReader(InputStream in) : 创建一个使用默认字符集的字符流。 InputStreamReader(InputStream in, String charsetName) : 创建一个指定字符集的字符流。
OutputStreamWriter类
转换流 java.io.OutputStreamWriter ,是Writer的子类,是从字符流到字节流的桥梁。使用指定的字符集将字符 编码为字节。它的字符集可以由名称指定,也可以接受平台的默认字符集。
构造方法
OutputStreamWriter(OutputStream in) : 创建一个使用默认字符集的字符流。OutputStreamWriter(OutputStream in, String charsetName) : 创建一个指定字符集

InputStreamReader读取字节解码转换为字符。
OutputStreamWriter把字符写出为字节。

序列化与反序列化
对象序列化的机制:用一个字节序列可以表示一个对象,该字节序列包含该 对象的数据 、 对象的 类型 和 对象中存储的属性 等信息。
字节序列写出到文件之后,相当于文件中持久保存了一个对象的信息。 反之,该字节序列还可以从文件中读取回来,重构对象,对它进行反序列化。
ObjectOutputStream与ObjectInputStream类
ObjectOutputStream
java.io.ObjectOutputStream 类将Java对象的原始数据类型写出到文件,实现对象的持久存储。
构造方法 :
public ObjectOutputStream(OutputStream out) : 创建一个指定OutputStream的ObjectOutputStream。
序列化操作必须满足两个条件: 该类必须实现 java.io.Serializable 接口, Serializable 是一个标记接口,不实现此接口的类将不会使任 何状态序列化或反序列化,会抛出 NotSerializableException 。 该类的所有属性必须是可序列化的。如果有一个属性不需要可序列化的,则该属性必须注明是瞬态的,使用 transient 关键字修饰。
构造方法
public ObjectInputStream(InputStream in) : 创建一个指定InputStream的ObjectInputStream。
对于JVM可以反序列化对象,它必须是能够找到class文件的类。如果找不到该类的class文件,则抛出一个 ClassNotFoundException 异常。
另外,当JVM反序列化对象时,能找到class文件,但是class文件在序列化对象之后发生了修改,那么反序列化操 作也会失败,抛出一个 InvalidClassException 异常。
Serializable 接口给需要序列化的类,提供了一个序列版本号。 serialVersionUID 该版本号的目的在于验证序 列化的对象和对应类是否版本匹配。

打印流
平时我们在控制台打印输出,是调用 print 方法和 println 方法完成的,这两个方法都来自于 java.io.PrintStream 类,该类能够方便地打印各种数据类型的值,是一种便捷的输出方式。
PrintStream类
构造方法 public PrintStream(String fileName) : 使用指定的文件名创建一个新的打印流。
改变打印流向 System.out 就是 PrintStream 类型的,只不过它的流向是系统规定的,打印在控制台上。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值