IOException:stream close异常问题产生的原因及解决方法

package com.haligong.Jse;

import java.io.*;
import java.util.Scanner;

/**
 * Created by Administrator on 2019/7/4.
 */
public class IOStream {
    public static void main(String[] args) {
//        FileCopy();
        //RWCopy();
        //BuffCopy();
        //inAndOutput1();
        try {
//            inAndOutput2();
            inAndOutput3();
//            对比一下inAndOutput4和inAndOutput3的写法
            //inAndOutput4();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    static private void FileCopy(){
        {
            FileInputStream fin =null;
            FileOutputStream fou =null;
            double ds=0;
            double de=0;
            try {
//            fin = new FileInputStream("D:\\Documents\\Downloads\\Y1\\这里是流的文件所在地\\1\\1.txt");
//            fou= new FileOutputStream("D:\\Documents\\Downloads\\Y1\\这里是流的文件所在地\\2\\2.txt");
                fin = new FileInputStream("D:\\Documents\\Downloads\\Y1\\这里是流的文件所在地\\1\\1.wmv");
                fou= new FileOutputStream("D:\\Documents\\Downloads\\Y1\\这里是流的文件所在地\\2\\2.wmv");
                int n=-1;
                byte [] buff= new byte[1024];
                ds=System.currentTimeMillis();
                while((n=fin.read(buff))!=-1){
                    fou.write(buff,0,n);
                    //fou.write("我爱我家".getBytes())
                }
                de=System.currentTimeMillis();



            } catch (java.io.IOException e) {
                e.printStackTrace();
            } catch(Exception e){
                System.out.println("以防万一");
            } finally{
                try {
                    fou.flush();
                    fin.close();
                    fou.close();
                    System.out.println(de-ds/1000);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
    static private void RWCopy(){
        Reader r=null;
        Writer w=null;
        try{
            r=new FileReader("D:\\Documents\\Downloads\\Y1\\这里是流的文件所在地\\1\\1.txt");
            w=new FileWriter("D:\\Documents\\Downloads\\Y1\\这里是流的文件所在地\\1\\3.txt");
            int n=-1;
            char [] buffer=new char[1024];
            while((n=r.read(buffer))!=-1){
                w.write(buffer);
            }
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            try {
                w.flush();
                w.close();
                r.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    static private void BuffCopy(){
        FileInputStream fin =null;
        FileOutputStream fou =null;
        BufferedInputStream bin =null;
        BufferedOutputStream bou=null;
        try{
            fin=new FileInputStream("D:\\Documents\\Downloads\\Y1\\这里是流的文件所在地\\1\\1.txt");
            fou=new FileOutputStream("D:\\Documents\\Downloads\\Y1\\这里是流的文件所在地\\2\\5.txt");
            bin=new BufferedInputStream(fin,4096);
            bou=new BufferedOutputStream(fou,4096);
            byte [] buffer=new byte[1024*2];
            int n=-1;
            while((n=bin.read(buffer))!=-1){
                bou.write(buffer,0,n);
            }
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            try {
                bou.flush();
                bou.close();

                fou.flush();
                fou.close();

                bin.close();
                fin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    static private void inAndOutput1(){
        Scanner s= new Scanner(System.in);

        while(true){
            String re= s.nextLine();
            if(re.toLowerCase().equals("q")){
                System.exit(0);
            }else{
                System.out.println(re);
            }
        }
    }
    static private void inAndOutput2(){
        InputStream i = System.in;
        while(true){
//            InputStream i = System.in;
            byte [] buffer=new byte[1024];
            try {
                i.read(buffer);//这个才是真正的读数据

                String s= new String(buffer);
                if(s.toLowerCase().trim().equals("q")){
                    System.exit(0);
                }else{
                    System.out.println(s.trim());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }catch(Exception e){
                e.printStackTrace();
            }finally{

//                try {
//                   i.close();   //不能关闭,否者会把Systme.in关闭了
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
            }
        }
    }
    static private void inAndOutput3(){
        InputStream i=null;
        BufferedReader r=null;
        i = System.in;
        while(true){
            r=new BufferedReader(new InputStreamReader(i));
            try {
                String s= r.readLine();
                if(s.trim().equals("q")){
                    System.exit(0);
                }
                System.out.println(s.trim());
            } catch (IOException e) {
                e.printStackTrace();
            } catch(Exception e){
                e.printStackTrace();
            } finally{//我这样把整个try catch都写里面了不对,会造成IOException:stream close  下面参考一下inAndOutput4()的写法
                try {
                    r.close();
                    i.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    static private void inAndOutput4(){
        InputStream i=null;
        BufferedReader r= null;
        try{
            i=System.in;
            r=new BufferedReader(new InputStreamReader(i));
            while(true){
                String s=r.readLine();
                if(s.trim().toLowerCase().equals("q")){//或者这样写s.trim().equalsIgnoreCase("q");
                    System.exit(0);
                }else{
                    System.out.println(s.trim());
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try {
                r.close();
                i.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

在运行程序时,会报错,如下图所示:

在这里插入图片描述
去stackoverflow中搜索了下发现原因:
在这里插入图片描述
他的意思就是说如果你把BufferedReader 输入流给关闭了,那么就就会把System.in也给关闭了,导致报出Stream close异常,解决的方法也简单,就是将不写close(),或者是改变一下写的顺序问题,参考一下inAndOutput4的写法就是将流的关闭放在while循环外面,每次循环并不会产生流的关闭,只有退出while循环的时候才关闭流

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值