02-、java03- 缓冲流、转换流、序列化流

1、高级流对象是对基本使用的四个流对象的进一步封装,以提供更为合适和方便功能

再次强调一下顶级流对象:[字节流] [字符流]
    --OutputStream:字节输出流
    --InputStream:字节输入流
    --Reader:字符输入流
    --Writer:字符输出流

初步使用的基础流对象:
    --FileOutputStream:文件输出流,用于将数据写出到文件  OutputStream
    --FileInputStream:文件输入流,从文件中读取字节  InputStream
    --FileReader:是读取字符文件的便利类  Reader
    --FileWriter:是写出字符到文件的便利类  Writer

2、缓冲字节流 --- 高效流,是对四个初步使用的流对象的增强

----分为四类 Buffered + 顶级父类名称
    --字节缓冲流:BufferedInputStream,BufferedOutputStream
    --字符缓冲流:BufferedReader,BufferedWriter

--缓冲流的基本原理,是在创建流对象时,会创建一个内置的默认大小的缓冲区数组,通过缓冲区读写,减少系统IO次数,从而提高读写的效率。

--Buffered 输入输出缓冲字节流构造:
    // 创建字节缓冲输入流
    --BufferedInputStream bis = new BufferedInputStream(new FileInputStream("bis.txt"));

    // 创建字节缓冲输出流
    --BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bos.txt"));

--程序睡眠等待
    --currentTimeMillis()返回以毫秒为单位的当前时间,返回的是当前时间与协调世界时 1970 年 1 月 1 日午夜之间的时间差(以毫秒为单位测量)

    long start = System.currentTimeMillis();
    try{
        Thread.sleep(1000);
    }catch(Exception ex){
        ... ...
    }
    long end = System.currentTimeMillis();
    System.out.println(end - start);

--下面是几种字符流时间比较:
    --最最基础的文本字符流:一个一个字节读取,一个一个字节写入文件,io操作频繁,每次读取一个字节
    --最最基础的文本字符流--读取一个含汉字的57.2MB的文本,代码示例如下:
        --结果:124661 ms
        --代码示例:
            long start = System.currentTimeMillis();  // 获取开始时间
            // 读入数据存入另一个文件
            FileInputStream f1 = new FileInputStream("D:\\代码\\java\\helloworld\\file_
data\\file_text.txt");
            FileOutputStream f2 = new FileOutputStream("D:\\代码\\java\\helloworld\\file_
data\\file_text_01.txt");

            while ((f1.read()) != -1){
                f2.write(f1.read());
            }

            long end = System.currentTimeMillis();  // 获取结束时间
            System.out.println(end - start);

    --最最基础的文本字符流--使用字节数组接受,字节数组导出
        --结果:1342 ms || 数组大小 100
               12862 ms || 数组大小 10
        --代码示例:
            long start = System.currentTimeMillis();  // 获取开始时间
            // 读入数据存入另一个文件
            FileInputStream f1 = new FileInputStream("D:\\代码\\java\\helloworld\\file_
data\\file_text.txt");
            FileOutputStream f2 = new FileOutputStream("D:\\代码\\java\\helloworld\\file_
data\\file_text_01.txt");

            byte[] get_byte = new byte[10];
            while ((f1.read(get_byte)) != -1){
                f2.write(get_byte, 0, f1.read(get_byte));
            }

            long end = System.currentTimeMillis();  // 获取结束时间
            System.out.println(end - start);

    --使用BufferedInputStream  和  BufferedOutputStream
        --结果:686 ms
        --代码示例:
            long start = System.currentTimeMillis();  // 获取开始时间
            try(
            BufferedInputStream in_buffer = new BufferedInputStream(new FileInputStream
("D:\\代码\\java\\helloworld\\file_data\\file_text.txt"));
            BufferedOutputStream out_buffer = new BufferedOutputStream(new FileOutputSt
ream("D:\\代码\\java\\helloworld\\file_data\\file_text_01.txt"));
            ){
                int b;
                while((b = in_buffer.read()) != -1){
                    out_buffer.write(b);
                }
            } catch (IOException e){
                e.printStackTrace();
            }
            long end = System.currentTimeMillis();  // 获取结束时间
            System.out.println(end - start);

    --使用BufferedInputStream  和  BufferedOutputStream 的数组形式:
        --结果:18 ms
        --代码示例:
            long start = System.currentTimeMillis();  // 获取开始时间
            try(
                BufferedInputStream in_buffer = new BufferedInputStream(new FileInput
Stream("D:\\代码\\java\\helloworld\\file_data\\file_text.txt"));
                BufferedOutputStream out_buffer = new BufferedOutputStream(new FileOut
putStream("D:\\代码\\java\\helloworld\\file_data\\file_text_01.txt"));
            ){
                int b;
                byte [] byte_buffer = new byte[20*1024];
                while((b = in_buffer.read(byte_buffer)) != -1){
                    out_buffer.write(byte_buffer, 0, b);
                }
            } catch (IOException e){
                e.printStackTrace();
            }
            long end = System.currentTimeMillis();  // 获取结束时间
            System.out.println(end - start);

3、缓冲字符流 --- 高效流,是对四个初步使用的流对象的增强

--构造方法:
    public BufferedReader(Reader in) :创建一个 新的缓冲输入流。
    public BufferedWriter(Writer out) : 创建一个新的缓冲输出流。

--构造方法 代码示例:
    // 创建字符缓冲输入流
    BufferedReader br = new BufferedReader(new FileReader("br.txt"));
    // 创建字符缓冲输出流
    BufferedWriter bw = new BufferedWriter(new FileWriter("bw.txt"));

--字符缓冲流的基本方法与普通字符流调用方式一致,不再阐述,我们来看它们具备的特有方法。
    --字符缓冲输入流基本方法:
        --read() 方法|无参数读取,每次可以读取一个字节的数据,提升为int类型,读取到文件末尾,返回 -1
        --read(byte [] b): 有一个字节数组,传入多大数组就一次读多少个数据返回
        --BufferedReader: public String readLine() : 读一行文字。

    --字符缓冲输入流示例:
        long start = System.currentTimeMillis();  // 获取开始时间
        BufferedReader bfr = new BufferedReader(new FileReader("D:\\代码\\java\\hello
world\\file_data\\file_text_02.txt"));
        String bfr_line = "";
        while((bfr_line = bfr.readLine()) != null){
            System.out.println(bfr_line);
        }
        bfr.close();
        long end = System.currentTimeMillis();  // 获取结束时间
        System.out.println(end - start);

    --字符缓冲输出流基本方法:
        --write(int b) 方法,每次可以写出一个字符数据
        --flush :刷新缓冲区,流对象可以继续使用。
        --close :先刷新缓冲区,然后通知系统释放资源。流对象不可以再被使用了。
        --BufferedWriter: public void newLine() : 写一行行分隔符,由系统属性定义符号,用来换行的

    --字符缓冲输出流示例:
        BufferedWriter bfw = new BufferedWriter(new FileWriter("D:\\代码\\java\\hello
world\\file_data\\file_text_01.txt"));
        String s = "的发表v都会有人·123456789?》《|“:{}|#¥%&……*&()~";
        bfw.write(s);
        bfw.newLine();

        String s1 = "黑马程序员";
        bfw.write(s1);
        bfw.newLine();

        bfw.close();
        

4、将一个文本中的数据读出再写入一个文件

"""
    qqqqqqqqqq
    6325492376
    :|“》?《》{}{|”》
    和v高度概括和部分v和
    wsfv不要和本科生的发挥v本人的1246图72379《》?《?》“:|{|}{”|:》:》?tttt
    ~+——“{}》*-/*:{L》:》《LP”OP{“OL》:!@@#¥%……&*)(~yyyyyyyyy
    uuuuuuuuu
    23462340128704467
    pppppp

    3.侍中、侍郎郭攸之、费祎、董允等,此皆良实,志虑忠纯,是以先帝简拔以遗陛下。愚以为宫中之事,事    无大小,悉
    以咨之,然后施行,必得裨补阙漏,有所广益。
    8.愿陛下托臣以讨贼兴复之效,不效,则治臣之罪,以告先帝之灵。若无兴德之言,则责攸之、祎、允等之慢,以彰其
    咎;陛下亦宜自谋,以咨诹善道,察纳雅言,深追先帝遗诏,臣不胜受恩感激。
    4.将军向宠,性行淑均,晓畅军事,试用之于昔日,先帝称之曰能,是以众议举宠为督。愚以为营中之事,悉以咨之,
    必能使行阵和睦,优劣得所。
    2.宫中府中,俱为一体,陟罚臧否,不宜异同。若有作奸犯科及为忠善者,宜付有司论其刑赏,以昭陛下平明之理,不
    宜偏私,使内外异法也。
"""


----代码示例:
    public static void main(String[] args) throws IOException {
        long start = System.currentTimeMillis();
        // 使用HashMap存储
        HashMap<String, String> hmap = new HashMap<>();

        // 定义流对象
        BufferedReader br = new BufferedReader(new FileReader("D:\\代码\\java\\helloworld
\\file_data\\file_test_04.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\代码\\java\\helloworld
\\file_data\\file_text_01.txt"));

        // 读入并存入HashMap中
        String line;
        while((line = br.readLine()) != null){
            String [] linesp = line.split("\\.");
            hmap.put(linesp[0], linesp[1]);
        }
        br.close();

        // 将HashMap中数据读入至文本文件中
        for(int i = 1;i<hmap.size();i++){
            // 将数字转化为字符,并当作键来取值
            String linebw = hmap.get(String.valueOf(i));
            bw.write(String.valueOf(i) + "." + linebw);
            bw.newLine();
        }
        bw.close();

        long end = System.currentTimeMillis();
        System.out.println("程序运行时间是:" + (end - start));
    }

5、转换流--编码

--字符编码:就是一套 数字 字符对应关系表
--字符集:一套乃至多套有一定联系的字符编码 
例如 unicode字符集:utf-8 utf-16 utf-32
        --utf8:
            --128个US-ASCII字符,只需一个字节编码。
            --拉丁文等字符,需要二个字节编码。
            --大部分常用字(含中文),使用三个字节编码。
            --其他极少使用的Unicode辅助字符,使用四字节编码。

     ascll字符集
        --基本为7位bit,扩展形式为8bit,即一个字节,正好对应byte数据类型

     gb**字符集:gbk gb2312 gb18030
        --gb2312:是2个字节

     iso-8859-1:
        --Latin-1:

6、转换流--

--理解:文本中不论以什么格式保存数据,本质都是二进制,最基本单位都是一个字节,一般默认为ascall utf8 gbk三种[字节 字符对应关系]。所以转换输入流类的参数应该是一个字节流,输出流类的参数应该也是一个字节流。

--如果字节流读写,就是使用最基本的ascll编码形式找出字节对应的ascll字符
--如果设置了gbk utf8则,表明会将字节组合找到对应字符并导入导出,也就是说字符流本质上是高级的字节流


--InputStreamReader类

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

--构造方法举例:
    --InputStreamReader isr = new InputStreamReader(new FileInputStream("in.txt"));
    --InputStreamReader isr2 = new InputStreamReader(new FileInputStream("in.txt") , "GBK
");

--代码示例:
    public static void main(String[] args) throws IOException{ 
        long start = System.currentTimeMillis();
        String Filename = "D:\\代码\\java\\helloworld\\file_data\\file_test_04.txt";

        // 默认编码格式和
        InputStreamReader isr = new InputStreamReader(new FileInputStream(Filename));
        InputStreamReader isr01 = new InputStreamReader(new FileInputStream(Filename), "GBK");

        // 默认编码格式 -- 文件格式为 gbk,所以按照默认utf8会读出乱码
        int read;
        while((read = isr.read())!=-1){
            System.out.println((char)read);
        }
        isr.close();

        // gbk编码格式
        int read01;
        while((read01 = isr01.read()) != -1){
            System.out.println((char)read01);
        }
        isr01.close();
        
        long end = System.currentTimeMillis();
        System.out.println("程序运行时间是:" + (end - start));
    }

--OutputStreamWriter类

--转换流 java.io.OutputStreamWriter ,是Writer的子类,是从字符流到字节流的桥梁。使用指定的字符集将字符编码为字节。它的字符集可以由名称指定,也可以接受平台的默认字符集。

--构造方法:
    --OutputStreamWriter(OutputStream in) : 创建一个使用默认字符集的字符流。
    --OutputStreamWriter(OutputStream in, String charsetName) : 创建一个指定字符集的字符流。

--构造方法举例:
    --OutputStreamWriter isr = new OutputStreamWriter(new FileOutputStream("out.txt"));
    --OutputStreamWriter isr2 = new OutputStreamWriter(new FileOutputStream("out.txt") , "GBK");

--代码示例:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值