zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    linux基础
    模块三、企业实战案例
    模块二、shell脚本逻辑结构
    模块一:shell 脚本基础
    三剑客、shell脚本
    定时任务、用户管理、磁盘介绍
    python笔记03
    文件属性、正则表达式、文件权限
    Linux系统目录结构介绍
    Linux基础及入门介绍
  • 原文地址:https://www.cnblogs.com/AI-Cobe/p/7880223.html
Copyright © 2011-2022 走看看