2013-10-21 183 views
1

继承人代码:这个FileWriter是如何工作的?

import java.io.*; 

public class FileCharCopier 
{ 
    public static void main(String args[]) throws IOException 
    { 

    File f1=new File("scjp.txt"); 
    File f2=new File("scjpcopy.txt"); 

    FileReader in=new FileReader(f1); 
    FileWriter out=new FileWriter(f2); 

    int c; 

    while((c=in.read())!=1) 
      { 
     out.write(c); 
     in.close(); 
     out.flush(); 
     out.close(); 


    } 
} 
} 

我在同一个目录都SCJP和scjpcopy.txt

但是当我运行该程序,我得到这些错误:

java.io.IOException: Stream closed 
    at sun.nio.cs.StreamEncoder.ensureOpen(Unknown Source) 
    at sun.nio.cs.StreamEncoder.write(Unknown Source) 
    at sun.nio.cs.StreamEncoder.write(Unknown Source) 
    at java.io.OutputStreamWriter.write(Unknown Source) 
    at FileCharCopier.main(FileCharCopier.java:18) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272) 

线18指的是

out.write(c); 

有人可以纠正错误吗?

+2

你已经关闭了第一个循环中的输入和输出流,所以在第二个循环中,它引起异常 – MadProgrammer

+0

除了注释中指出的基本错误以外,您肯定不应该在时间。应该使用一个字符数组: '而((计数= in.read(阵列))> 0)out.write(阵列,0,计数);'这是很多倍。或者是一个BufferedReader和一个BufferedWriter。 – EJP

回答

0

从while循环中删除行in.close()out.close(),使之像这样:

while((c=in.read())!=1) 
{ 
    out.write(c); 
    out.flush(); 
} 

它的作用:从文件中读取一个字符后,它关闭阅读器以便in没有更多的输入文件附并抛出异常。

参考this甲骨文的文档。这是写的是:
Closes the stream and releases any system resources associated with it. Once the stream has been closed, further read(), ready(), mark(), reset(), or skip() invocations will throw an IOException. Closing a previously closed stream has no effect.

0

把你out.close();in.close() while循环之外。

它每次关闭的OutputStream。写完所有东西之后,可以在while循环之外关闭它。

希望那是有帮助的。

0

您关闭流,而你还在试图写入them.Move清理过程中跳出循环。

import java.io.*; 
public class FileCharCopier 
{ 
    //...same 
    while((c=in.read())!=1){ 
      out.write(c); 
      out.flush(); 
    } 
      in.close();    
      out.close(); 
    } 
} 
3

基本上,你正在关闭输入和输出上while-loop的第一次迭代蒸。这意味着当您尝试第二次循环时,流将关闭,并抛出异常。

相反,当你与他们所做的,比如你应该关闭流。

public class FileCharCopier { 

    public static void main(String args[]) throws IOException { 

     File f1 = new File("scjp.txt"); 
     File f2 = new File("scjpcopy.txt"); 

     FileReader in = null; 
     FileWriter out = null; 

     try { 

      in = new FileReader(f1); 
      out = new FileWriter(f2); 

      int c; 

      while ((c = in.read()) != 1) { 
       out.write(c); 
      } 

      out.flush(); 

     } finally { 
      try { 
       out.close(); 
      } catch (Exception e) { 
      } 
      try { 
       in.close(); 
      } catch (Exception e) { 
      } 
     } 
    } 
} 

这将使用try-finally块,以确保所有最好的努力是由既可以当回路中存在或产生了某种错误关闭流。现在

,如果你够幸运,使用Java 7是,你可以使用try-with-resources语句来代替,例如...

public class FileCharCopier { 

    public static void main(String args[]) throws IOException { 

     File f1 = new File("scjp.txt"); 
     File f2 = new File("scjpcopy.txt"); 

     try (FileReader in = new FileReader(f1); FileWriter out = new FileWriter(f2)) { 

      int c; 

      while ((c = in.read()) != 1) { 
       out.write(c); 
      } 

      out.flush(); 

     } finally { 
     } 
    } 
} 

The try-with-resources statement看看更多细节