Java>InputStreamReader ()和OutputStreamWriter ()使用方法

InputStreamReader ()
OutputStreamWriter ()

    public static void main(String[] args) throws IOException {
        method_01();
    }

    public static void method_01() throws IOException {
        // InputStreamReader dome  转换流
        // E:\folder_io_dome\domeDir03\a2.txt
        FileInputStream fileInputStream = new FileInputStream("E:\\folder_io_dome\\domeDir03\\a2.txt");
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "GBK");

        // will data save memory
        String data = new String();

        int len = 0;
        char[] char_arr = new char[1024];
        while ((len = inputStreamReader.read(char_arr)) != -1) {
            System.out.println(new String(char_arr, 0, len));
            // save data
            data = new String(char_arr, 0, len);
        }
        String[] split = data.split("\n");
        System.out.println(split.length);

        // close stream 关闭流
        fileInputStream.close();
        inputStreamReader.close();

        // OutputStreamWrite dome  转换流 将内存中的字符串写入到硬盘中
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("E:\\folder_io_dome\\domeDir03\\a3.txt"), "UTF-8");

        for (int i = 0; i < split.length; i++) {
            for (int i1 = 0; i1 < split[i].toCharArray().length; i1++) {
                outputStreamWriter.write(split[i].toCharArray()[i1]);
            }
        }
        outputStreamWriter.flush();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值