zoukankan      html  css  js  c++  java
  • IO流11 --- 缓冲流(字符型)实现文本文件的复制 --- 技术搬运工(尚硅谷)

    • 方法一:读到指定字符数组
    @Test
    public void test7(){
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new FileReader("射雕英雄传.txt"));
            bw = new BufferedWriter(new FileWriter("射雕英雄传1.txt"));
    
            char[] cbuf = new char[1024];
            int len;
            while ((len = br.read(cbuf)) != -1){
                bw.write(cbuf, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    • 方法2:一次读一行
    @Test
    public void test8(){
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new FileReader("射雕英雄传.txt"));
            bw = new BufferedWriter(new FileWriter("射雕英雄传1.txt"));
    
            String data;
            while ((data = br.readLine()) != null){
                bw.write(data);//不包括换行
                bw.newLine();//换行
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

      

  • 相关阅读:
    使用XStream解析xml
    分享功能
    上拉加载 下拉刷新
    点击button倒计时
    正则表达式验证手机号码
    第三方登陆
    test
    横向滑动菜单HorizontalScrollView
    slidingmenu侧滑侧单
    2017/4/25 afternoon
  • 原文地址:https://www.cnblogs.com/noyouth/p/11714126.html
Copyright © 2011-2022 走看看