Java IO流--转换流InputStreamReader和OutputStreamWriter的使用

博主前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住也分享一下给大家,
👉点击跳转到网站

转换流的使用:

1、转换流属于处理流

InputStreamReader :将一个字节的输入流转换为字符的输入流
OutputStreamWriter:将一个字节的输出流转换为字符的输出流

2、提供字节流与字符流之间的转换

3、解码:字节、字节数组—>字符数组,字符串
编码:字符数组,字符串---->字节,字节数组

4、字符集

一、InputStreamReader的使用,具体代码如下:

/**
 * 演示使用InputStreamReader 转换流解决中文乱码问题
 * 将字节流FileInputStream转成字符流InputStreamReader,指定编码gbk/utf-8
 */
public class InputStreamReader_ {
    public static void main(String[] args) {
        String filePath = "D:\\a.txt";
        InputStreamReader isr = null;
        try {
            //1.把FileInputStream转成InputStreamReader
            //2.指定编码gbk
            //isr = new InputStreamReader(new FileInputStream(filePath), "gbk");
            //3.把InputStreamReader传入BufferedReader
            //BufferedReader bufferedReader = new BufferedReader(isr);

            //将2和3合在一起
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
                    new FileInputStream(filePath), "gbk"));
            //4.读取
            String data = bufferedReader.readLine();
            System.out.println("data=" + data);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (isr != null) {
                    isr.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

二、OutputStreamWriter的使用,具体代码如下:

/**
 * 演示OutputStreamWriter使用
 * 把FileOutputStream字节流,转成字符流OutputStreamWriter
 * 指定处理的编码gbk/utf-8/utf8
 */
public class OutputStreamWriter_ {
    public static void main(String[] args) {
        String filePath = "D:\\b.txt";
        OutputStreamWriter osw = null;
        try {
            osw = new OutputStreamWriter(new FileOutputStream(filePath), "utf8");
            osw.write("hello,筱路");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (osw != null) {
                    osw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

三、InputStreamReader 将一个字节的输入流转换为字符的输入流

@Test
    public void test1(){

        InputStreamReader isr = null;
        try {
            FileInputStream fis = new FileInputStream("学习记录.txt");
            isr = new InputStreamReader(fis); //使用系统默认的字符集
            //参数2:指明了字符集,具体使用哪个字符集,取决于 学习文件.txt 保存时 使用的字符集
            isr = new InputStreamReader(fis,"UTF-8");

            char[] cbuf = new char[10];
            int len;
            while ((len= isr.read(cbuf))!=-1){
                String str = new String(cbuf,0,len);
                System.out.println(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (isr!=null){
                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

四、将一个字符的输出流转换为一个字节的输出流

@Test
    public void test2() {
        InputStreamReader isr = null;
        OutputStreamWriter osw = null;
        try {
            //1.造文件、造流
            File file1 = new File("学习记录.txt");
            File file2 = new File("学习记录5.txt");

            FileInputStream fis = new FileInputStream(file1);
            FileOutputStream fos = new FileOutputStream(file2);

            isr = new InputStreamReader(fis,"UTF-8");
            osw = new OutputStreamWriter(fos,"GBK");
            //2.读取,写入过程
            int len;
            char[] cbuf = new char[5];
            while ((len= isr.read(cbuf))!=-1){
                osw.write(cbuf,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (isr!=null){
                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (osw!=null){
                try {
                    osw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

路宇

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

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

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

打赏作者

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

抵扣说明:

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

余额充值