java.io.IOException: Stream closed

package av.code.thinking;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

//:字符输入流
public class FileReaderDemo{
    public static void main(String[] args) {
        FileReader fr = null;
        FileWriter fw = null;
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            fr = new FileReader("earn.txt");
            br = new BufferedReader(fr);
            fw = new FileWriter("call.txt");
            bw = new BufferedWriter(fw);
            
            String str = null;
            while(null != (str = br.readLine())) {
                bw.write(str);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != fr) {
                    fr.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                if(null != fw) {
                    fw.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                if(null != br) {
                    br.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                if(null != bw) {
                    bw.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
}
因为我在关闭 bw 流的时候会用到 fw 流,但是 fw 流已经被关闭了,所以会出现异常,解决办法就是 先关闭 bw 流,为什么呢?这又要扯到 节点流和处理流了,转载一篇博客http://blog.csdn.net/u014617239/article/details/52252438

 

posted @ 2017-11-22 17:13  一凡夫一俗子  阅读(3367)  评论(0编辑  收藏  举报