File Writer写入方法

时间:2019-02-18 09:19:08

标签: java exception stream

我目前下面具有以下代码的代码用于编写响应,并且最近我遇到了java.io.IOException:即使我正确关闭了File Writer,也经常出现流关闭错误,但不确定为什么有人可以帮助我。

Exception:
Caused by: java.io.IOException: Stream closed
    at sun.nio.cs.StreamEncoder.ensureOpen(StreamEncoder.java:26)
    at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:99)
    at java.io.OutputStreamWriter.write(OutputStreamWriter.java:190)
    at com.test.resp.RespWriter.write(RespWriter.java:150)
    at com.ctc.wstx.sw.BufferingXmlWriter.flushBuffer(BufferingXmlWriter.java:1103)
    at com.ctc.wstx.sw.BufferingXmlWriter.flush(BufferingXmlWriter.java:213)
    at com.ctc.wstx.sw.BufferingXmlWriter.close(BufferingXmlWriter.java:194)
    at com.ctc.wstx.sw.BaseStreamWriter.finishDocument(BaseStreamWriter.java:1690)

我在以下地方遇到异常的地方

fileWriter.write(buff,off,len);这是我遇到异常的地方

public void write(char[] buff, int off, int len) throws IOException {
            printWriter.write(buff, off, len);
            if(count < FILE_SIZE) {                
                fileWriter.write(buff, off, len); Here is the place I am getting Exception 
                count+=len;
            }
}

代码:

public RespWriter(RequestHeader header, PrintWriter printWriter)
 throws InitializationException {
  isClosed = false;
    if(StringUtil.isEmpty(FILE_PATH) || printWriter == null) {
        throw new InitializationException(
            InitializationException.ERR_CODE);
    } else {
        this.header = header;


        File responseFolder = new File(FILE_PATH.concat(
            "/".concat(header.getUserId())));

        boolean dirCreated = responseFolder.mkdir();            

        if(responseFolder.exists() && responseFolder.isDirectory()) {
            responseFile = new File(responseFolder,
                "R"+header.getId()+FILE_EXTENSION);
        } else {
            throw new InitializationException(
                InitializationException.ERR_CODE,
                "response folder does not exist");
        }
    this.printWriter = printWriter;
        try {
            fileWriter = new FileWriter(responseFile);
        } catch (IOException e) {
            log.error("FileWriter could not be created", e);
        }

        writeHeaderToFile();
    }
}

private void writeHeaderToFile() {
        String headerStr = "\nRequest Header:\nnull\n";
        try {
            if(header!=null) {
                headerStr="\n"+Thread.currentThread().getName()
                    +" Request Header:\n" + header.toString() + "\n";
            }
            fileWriter.write(headerStr, 0, headerStr.length());
            fileWriter.write("Date:");
            fileWriter.write(new Date().toString()+"\n");
            fileWriter.write("Response:\n"+Thread.currentThread().getName()+": ");
            fileWriter.flush();

        } catch (IOException e) {
            log.error("IOException occurred writing header to file", e);
        }
}

      @Override
    public void close() throws IOException {

        if(!isClosed) {
            try {
                if(fileWriter!=null) {
                    try {
                        fileWriter.close();
                    } catch(IOException e) {
                        log.error("IOException", e);
                    }
                }
                if(printWriter!=null) {
                    try {
                        printWriter.close();
                    } catch(IOException e) {
                        log.error("IOException", e);
                    }
                }
            } finally {
                isClosed = true;
            }
        }
}
public void flush() throws IOException {
            if(printWriter!=null) {
                try {
                    printWriter.flush();
                } catch(IOException e) {
                    log.error("IOException", e);
                }
            }
            if(fileWriter!=null) {
                try {
                    fileWriter.flush();
                } catch(IOException e) {
                    log.error("IOException", e);
                }
            }
}

我在代码中错过的任何事情,或者我想在Write方法中创建新的FileWriter实例。请帮忙。在此先感谢

1 个答案:

答案 0 :(得分:2)

进入write()方法时,流已经关闭。我建议您使用println 3种不同的调试语句来发现问题。在打开/创建拖缆时为1,在关闭拖缆时为2,在尝试对其进行写操作时为3,因此可以看到操作顺序。另外,在这里看看:java IO Exception: Stream Closed