黑马程序员技术交流社区

标题: JAVA IO流关闭后报错 [打印本页]

作者: saiqqww234    时间: 2016-5-5 20:46
标题: JAVA IO流关闭后报错
public void methodCopy() throws Exception{
File file1=new File("C:\\Users\\Liu\\Desktop\\【必看】资料来源.txt");
File file2=new File("C:\\Users\\Liu\\Desktop\\【必看】资料来源1.txt");
//1.使用字节流实现文本文件的复制
FileInputStream fis=new FileInputStream(file1);
FileOutputStream fos=new FileOutputStream(file2);
    InputStreamReader read=new InputStreamReader(fis);
    BufferedReader br=new BufferedReader(read);
    OutputStreamWriter write=new OutputStreamWriter(fos);
    BufferedWriter bw=new BufferedWriter(write);
    char[] c=new char[1024];
   
    int len;
    while((len=read.read(c))!=-1){
            bw.write(c, 0, len);
            bw.flush();
            if(fos!=null){
           
             bw.close();
            }
            if(fis!=null){
           
             br.close();
            }
    }
}


Exception in thread "main" java.io.IOException: Stream closed
at sun.nio.cs.StreamDecoder.ensureOpen(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.Reader.read(Unknown Source)
at at.xupt.liu.FileCopy.methodCopy(TestFileCopy.java:35)
at at.xupt.liu.TestFileCopy.main(TestFileCopy.java:17)




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2