IO里的转换流

转换流

将字节流转换为字符流:使用了装饰者设计模式

将字节输入流,转换为字符输入流:

public class Demo2 {
    public static void main(String[] args) throws IOException {
        FileInputStream fis =new FileInputStream("d://b.txt");
        InputStreamReader isr =new InputStreamReader(fis);
        char[] chars = new char[15];
        int len = isr.read(chars);
        //将内容读取到chars字符数组里
        isr.read(chars);
        System.out.println(new String(chars,0,len));
        isr.close();
    }
}

将字节流转换为字符流:使用了装饰者设计模式

将字节输入流,转换为字符输入流:

public class InputStreamReader {
    public static void main(String[] args) throws IOException {
        //这是创建了新文件流
        FileOutputStream fis = new FileOutputStream("d://c.txt");
        //创建字符流,向上面那个文件里写
        OutputStreamWriter osw = new OutputStreamWriter(fis);
        osw.write("天山鸟飞绝");
        osw.flush();
        osw.close();
    }
}

输出结果:D盘新建了c.txt文件,并且里面有”天山鸟飞绝“内容、

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值