缓冲流和转换流的使用【 File类+IO流知识回顾③】

缓冲流

简单的说就是缓冲流在原有的流上进一步增强,读写效率更高
字节缓冲流:BufferedInputStream, BufferedOutputStream
字符缓冲流:BufferedReader, BufferedWriter

字节缓冲流

BufferedInputStream 构造方法:
image.png
BufferedOutputStream 构造方法:
image.png
构造方法使用:

BufferedInputStream bis = new BufferedInputStream(new FileInputStream("文件位置"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("文件位置"));

字节缓冲流复制文件和普通输入输出流对比

public static void fileCopy1(String sourceFilePath,String targetFilePath){
    // 1.创建输入输出流对象
    FileInputStream fis = null;
    FileOutputStream fos = null;
    long start = System.currentTimeMillis(); // 复制开始起始时间
    try{
        fis = new FileInputStream(sourceFilePath);
        fos = new FileOutputStream(targetFilePath);

        // 2.定义len和byte数组
        int len;
        byte b[] = new byte[1024];

        // 3.循环读入并且输出
        while ((len=fis.read(b))!=-1){
            fos.write(b,0,len);
        }
    }catch (IOException e){
        System.out.println(e.getMessage());
    }finally {
        long end = System.currentTimeMillis(); // 复制结束时间
        try{
            if(fis!=null){
                fis.close();
            }
            if(fos!=null){
                fos.close();
            }
        }catch (IOException e){
            System.out.println(e.getMessage());
        }
        long during = end - start;

        System.out.println("copy完毕,耗时"+during);
    }
}

public static void fileCopy2(String sourceFilePath,String targetFilePath) throws IOException {
    // 创建流对象
    FileInputStream fis = new FileInputStream(sourceFilePath);
    FileOutputStream fos = new FileOutputStream(targetFilePath);
    // 创建缓冲流对象
    BufferedInputStream bis = new BufferedInputStream(fis);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    // 设置len和byte数组
    int len;
    byte b[] = new byte[1024];

    long start = System.currentTimeMillis();

    while ((len=bis.read(b))!=-1){
        bos.write(b,0,len);
    }
    bis.close();
    bos.close();

    long end = System.currentTimeMillis();

    System.out.println("copy完毕,耗时"+(end-start));
}

main方法测试,缓冲流比普通流快了3倍,这还是在复制185MB文件的情况下,

public static void main(String[] args) throws IOException {
    // 测试文件地址 "D:\download\baidudisk\jdk-8u211-linux-x64.tar.gz"  185MB
    String srcPath = "D:\\download\\baidudisk\\jdk-8u211-linux-x64.tar.gz";
    String optPath = "D:\\jdk.tar.gz.bk";
    // fileCopy1(srcPath,optPath); // copy完毕,耗时1101

    fileCopy2(srcPath,optPath); // copy完毕,耗时337
}

字符缓冲流

BufferedReader
image.png
BufferedWriter
image.png
构造方法使用:

BufferedReader bfr = new BufferedReader(new FileReader("文件位置"));
BufferedWriter bfw = new BufferedWriter(new FileWriter("文件位置"));

字符缓冲流的基本方法与普通字符流调用方式一致,但是字符缓冲流具备特有方法:

  1. BufferedReader : public String readLine() 读取一行数据,读取到最后返回null
  2. BufferedWriter:public void newLine() 换行,由系统属性定义符号
String filePath = "D:\\bfr.txt";
FileReader fr = null;
fr = new FileReader(filePath);
BufferedReader bfr = new BufferedReader(fr);
String readLine = null;
while ((readLine = bfr.readLine())!=null) {
    System.out.println(readLine);
}
bfr.close();
String filePath = "D:\\bfw.txt";
BufferedWriter bfw  = new BufferedWriter(new FileWriter(filePath));
// 先输出三次换行
bfw.newLine();
bfw.newLine();
bfw.newLine();
// 输出内容
bfw.write("中文。wwwwww");
bfw.close();


转换流

image.png
转换流即处理字节和字符的转换,也称之为编码和解码。
编码:字符->字节
解码:字节->字符

需要会使用的两个类 InputStreamReader 和 OutputStreamWriter ,从字面意思上不难理解,InputStreamReader 是 字节流到字符流的一个桥梁,OutputStreamWriter则是字符流到字节流的桥梁


InputStreamReader

image.png
InputStreamReader是Reader的子类。

  1. 构造方法
方法名称功能描述
public InputStreamReader(InputStream in)创建一个使用默认字符集的字符流
public InputStreamReader(InputStream in, final String enc)创建一个指定字符集的字符流
public InputStreamReader(InputStream in, CharsetDecoder dec)创建一个指定解码字符集的字符流
public InputStreamReader(InputStream in, Charset charset)创建一个指定字符集的字符流
  1. 利用转换流解决编码问题

编码问题的引入
首先,写一个txt,编码方式为GB18030,然后尝试使用字符缓冲流进行读入。

image.png

String filePath = "D:\\bfr2.txt";
BufferedReader bfr = new BufferedReader(new FileReader(filePath));
String line = null;
while ((line = bfr.readLine())!=null){
    System.out.println(line);
}
bfr.close();

从控制台窗口中看到,读取的字符都乱码了…此时为了应对不同编码文件的读入,我们就该使用字符转换流了。

image.png


下面是字符转换流的写法:

String filePath = "D:\\bfr2.txt";
InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath), "GBK");

int len ; 
while ((len= isr.read())!=-1){
    System.out.print((char) len);
}
isr.close();

image.png
字符转换流完美的解决了因为编码不同导致乱码的问题。


OutputStreamWriter

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

  1. 构造方法
public OutputStreamWriter(OutputStream out)创建一个使用默认字符集的字符流
public OutputStreamWriter(OutputStream out, String charsetName)创建一个指定字符集的字符流
public OutputStreamWriter(OutputStream out, Charset cs)创建一个指定字符集的字符流
public OutputStreamWriter(OutputStream out, CharsetEncoder enc)创建一个指定编码的字符流
  1. 指定编码输出文本
String optFilePath = "D:\\opsw.txt";
OutputStreamWriter opsw = new OutputStreamWriter(new FileOutputStream(optFilePath),"GBK");
opsw.write("中文-------编码GBK....");
opsw.write("123456");
opsw.write("abcdef");
opsw.close();

image.png


  • 18
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杀死一只知更鸟debug

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值