21.InputStream、高效字节流、字符流、转换流(InputStreamReader、OutPutStreamWriter)

一、InputStream

1.1.简介

字节输入流的超类

1.2常用方法

close、read

1.3子类

FileInputStream

ObjectInputStream

1.4

FileInputStream

1.4.1简介

从文件系统的某个文件中获得输入字节==>文件输入流

一般用于操作字节,一些以字节存储的图像数据,视频数据等

读取字符流考虑使用FileReader

1.4.2构造方法

FileInputStream(String path)

FileInputStream(File file)

1.4.3构造方法的两件事

①实例化FileInputStream对象

②将FileInputStream指向磁盘中文件

1.4.4使用步骤

①实例化FileInputStream对象

②调用读的方法

③关闭流资源

1.4.5一个字节一个字节读

1.4.6读取的原理

1.4.7改造:使用循环一个字节一个字节的读

1.4.8定义一个数组,一次读取多个字节

1.4.9改造:定义一个长度更大的数组进行读

1.4.10 read()和read(b)的比较

read()返回的是读取的字节 一个字节一个字节的读,效率低

read(b)返回的是读取的有效字节的个数 每次读取数组长度个数的字节,效率高

二、高效流(缓冲流)

2.1简介

1.高效读与高效写  BufferedInputStream  BufferedOutputStream

2.就是将OutputStream和InputStream进行了封装,提供了一个长度为8192的数组

3.每次需要获取数据时,从缓冲数组(内存)中获取,避免与磁盘进行多次交互

2.2构造方法

2.3高效流-读

2.4高效流-写

测试高效流的高效性

package com.qf.stu.demo3;

import java.io.*;

public class Test {
    public static void main(String[] args) throws IOException {
        long l = System.currentTimeMillis();
        Bufferedtest(new File("D:\\a\\b\\c\\2.jpg"),new File("D:\\a\\b\\c\\1.jpg"));
        long l1 = System.currentTimeMillis();
        System.out.println(l1-l);
        test(new File("D:\\a\\b\\c\\2.jpg"),new File("D:\\a\\b\\c\\1.jpg"));
        long l2 = System.currentTimeMillis();
        System.out.println(l2-l1);
    }
    public static void Bufferedtest(File infile,File outfile) throws IOException {
        InputStream is=new FileInputStream(infile);
        BufferedInputStream bis=new BufferedInputStream(is);
        OutputStream os=new FileOutputStream(outfile);
        BufferedOutputStream bos=new BufferedOutputStream(os);
        int len=-1;
        while ((len=bis.read())!=-1){
            bos.write(len);
        }
        bos.flush();
        bos.close();
        bis.close();
        os.close();
        is.close();
    }
    public static void test(File infile,File outfile) throws IOException {
        InputStream is=new FileInputStream(infile);
        OutputStream os=new FileOutputStream(outfile);
        int len=-1;
        while ((len=is.read())!=-1){
            os.write(len);
        }
        os.flush();
        os.close();
        is.close();
    }
}

 

2.5关闭资源 

1.从里到外进行关闭,最后使用的最先关闭

2.防止在执行中代码出现异常导致无法关闭资源 选择将异常捕获:try-catch-finally(Ctrl+Alt+T)

3.在关闭资源前还需要进行非空验证,否则可能会出现空指针异常

package com.qf.stu.demo1;

import java.io.*;

public class Test6 {
    public static void main(String[] args) {
        Reader r = null;
        Writer w = null;
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            r = new FileReader(new File("D:\\a\\1.txt"));
            w = new FileWriter(new File("D:\\a\\3.txt"));
            br = new BufferedReader(r);
            bw = new BufferedWriter(w);
            int length = 0;
            while ((length = br.read()) != -1) {
                bw.write(length);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bw!=null){
                    bw.close();
                }
                if (br!=null){
                    br.close();
                }
                if (w!=null){
                    w.close();
                }
                if (r!=null){
                    r.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

三、字符流

1.简介

用于操作字符

使用字节流来读写字符时,需要将字节转换为字符,转换中容易出现乱码,所以采用字符流来操作字符

2.编码表

ASCII 码表:主要提供了一些英文的字符所有的码表都遵循ASCII码表的规则

GBK码表

Unicode

UTF-8

3.3字符串的编码解码

getbyte(Charset charset)将字符串按照指定的编码格式转换为byte[]数组

String(byte[] byte,Charset charset)将字节数组按照指定的编码格式转换为字符串

String(byte[] byte,String charsetName)

3.4字符流-写Writer

3.4.1简介

子类必须实现的方法仅有 write(char[], int, int) flush() close() 。但是,多数子类将重写此处定义的一些方法,以提供更高的效率和/ 或其他功能。

3.4.2常规的一些方法

 3.5FilerWriter 

1.简介

用来写入字符文件的便捷类

2.构造方法

3.6字符流读Reader

1.简介

用于读取字符流的抽象类

子类必须实现的方法只有 read(char[], int, int) close() 多数子类将重写此处定义的一些方法,以提供更高的效率和/ 或其他功能

 常用的方法

 3.7文件字符流读 FileReader

1.简介

用来读取字符文件的便捷类

2.构造方法

 四、转换流

InputStreamReader

1.简介

是字节流通向字符牛顿桥梁

为达到最高效率,可考虑在BufferedReader 内包装InputStreamReader

 2.构造方法

 OutputStreamWriter

1.简介

是字符流通向字节流的桥梁

可以考虑将OutputStreamWriter包装到BufferedWriter

2.构造方法

 五、总结

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值