1

这是一个代码:

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);

有人可以纠正错误吗?

4

4 回答 4

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 {
        }
    }
}

查看声明try-with-resources获取更多详细信息

于 2013-10-21T05:05:15.870 回答
0

从 while 循环中删除该行in.close()out.close()使其如下所示:

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

它的作用:从文件中读取一个字符后,它会关闭阅读器,因此in不再附加输入文件并引发异常。

请参阅Oracle 文档。它写道:
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.

于 2013-10-21T05:03:07.420 回答
0

将您的out.close();andin.close()放在 while 循环之外。

它每次都关闭输出流。写完所有的东西后,你可以在while循环之外关闭它。

希望这会有所帮助。

于 2013-10-21T05:05:06.873 回答
0

当您仍在尝试向它们写入时,您正在关闭流。将清理过程移出循环。

import java.io.*;
public class FileCharCopier
{
  //...same
    while((c=in.read())!=1){
          out.write(c);
          out.flush();
    }
          in.close();              
          out.close();
    }
}
于 2013-10-21T05:05:08.403 回答