InputStream 、 InputStreamReader和BufferedReader(out同理)

一、InputStream 、 InputStreamReader和BufferedReader的区别

  1. InputStream :是所有字节输入流的超类,一般使用它的子类:FileInputStream等,它能输出字节流;
  2. InputStreamReader :是字节流与字符流之间的桥梁,能将字节流输出为字符流,并且能为字节流指定字符集,可输出一个个的字符;
  3. BufferedReader :提供通用的缓冲方式文本读取,readLine读取一个文本行, 从字符输入流中读取文本,缓冲各个字符,从而提供字符、数组和行的高效读取。

二、示例

  1、InputStream

   

package src;


import java.io.*;

public class FileIO {
    public static void main(String[] args) throws IOException {
        int i=0;
        File file=new File("D:/liuyifei.jpeg");
        FileInputStream fileInputStream=new FileInputStream(file);
        int available = fileInputStream.available();
        FileOutputStream fileOutputStream=new FileOutputStream(new File("D:/liuyifei.txt"));
        byte[] bytes=new byte[available];
        while(fileInputStream.read()!=-1){
            fileInputStream.read(bytes);
        }
        System.out.println(bytes);
        fileOutputStream.write(bytes);
        FileInputStream fileInputStream1=new FileInputStream(file);
        byte[] bytes1=new byte[available];
        fileInputStream1.read(bytes1);
        FileOutputStream fileOutputStream1=new FileOutputStream(new File("D:/beautifuls.jpeg"));
        fileOutputStream1.write(bytes1);
    }
    }

2、BufferedReader

import sun.misc.BASE64Decoder;

import java.io.*;

public class Base64 {
    public static void main(String[] args) throws IOException {
        String fileName = "img_base64.txt";
        File file = new File(fileName);
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        StringBuffer stringBuffer = new StringBuffer();
        String buffer;
        while ((buffer = bufferedReader.readLine()) != null) {
            stringBuffer.append(buffer);
        }
        BASE64Decoder base64decoder = new BASE64Decoder();
        byte[] bytes = base64decoder.decodeBuffer(stringBuffer.toString());
        FileOutputStream outputStream = new FileOutputStream(new File("genrerate.png"));
        outputStream.write(bytes);
    }
}

3、InputStreamReader

package src;


import java.io.*;

public class FileIO {
    public static void main(String[] args) throws IOException {
        int i=0;
        File file=new File("D:/liuyifei.jpeg");
        FileInputStream fileInputStream=new FileInputStream(file);
        int available = fileInputStream.available();
/*        FileOutputStream fileOutputStream=new FileOutputStream(new File("D:/liuyifei.txt"));
        byte[] bytes=new byte[available];
        while(fileInputStream.read()!=-1){
            fileInputStream.read(bytes);
        }
        System.out.println(bytes);
        fileOutputStream.write(bytes);
        FileInputStream fileInputStream1=new FileInputStream(file);
        byte[] bytes1=new byte[available];
        fileInputStream1.read(bytes1);
        FileOutputStream fileOutputStream1=new FileOutputStream(new File("D:/beautifuls.jpeg"));
        fileOutputStream1.write(bytes1);*/
        InputStreamReader inputStreamReader=new InputStreamReader(fileInputStream);
        while (inputStreamReader.read()!=-1){
            System.out.println(inputStreamReader.read());
        }
    }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>