这个FileWriter如何工作?

时间:2013-10-21 04:59:42

标签: java filereader

下载代码:

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 个答案:

答案 0 :(得分: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了解详情

答案 1 :(得分:0)

从while循环中删除行in.close()out.close()并将其设为:

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

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

参考this 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.

答案 2 :(得分:0)

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

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

希望有帮助。

答案 3 :(得分:0)

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

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