zoukankan      html  css  js  c++  java
  • IO流13 --- 转换流实现文件复制 --- 技术搬运工(尚硅谷)

    InputStreamReader 将字节输入流转换为字符输入流

    OutputStreamWriter 将字符输出流转换为字节输出流

    @Test
    public void test2() {
        //转换流
        InputStreamReader isr = null;
        OutputStreamWriter osw = null;
        
        try {
            //节点流
            FileInputStream fis = new FileInputStream("水浒传.txt");
            FileOutputStream fos = new FileOutputStream("水浒传_gbk.txt");
            //转换流
               isr = new InputStreamReader(fis, "utf-8");
            osw = new OutputStreamWriter(fos, "gbk");
            //复制文件
            char[] cbuf = new char[20];
            int len;
            while ((len = isr.read(cbuf)) != -1) {
                osw.write(cbuf, 0, len);
            } 
            System.out.println("复制成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(osw != null) {
                try {
                    osw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(isr != null) {
                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
  • 相关阅读:
    117. Populating Next Right Pointers in Each Node II
    116. Populating Next Right Pointers in Each Node
    DFS & BFS
    Binary Search
    博客保存
    python强大的正则表达式
    游戏注意的地方
    vim使用
    下一步的
    lua的动态特性
  • 原文地址:https://www.cnblogs.com/noyouth/p/11727825.html
Copyright © 2011-2022 走看看