Java IOException - 流已关闭

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32943897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:03:37  来源:igfitidea点击:

Java IOException - Stream Closed

javaioexception

提问by Scitech

I get "IOException: Stream Closed"when I run this program. The text contains many lines of data. Program should read each line, do necessary function and write the output to a new file. I am confused as to which writer should be closed first and where.

当我运行这个程序时,我得到IOException:Stream Closed”。文本包含多行数据。程序应该读取每一行,执行必要的功能并将输出写入新文件。我对应该首先关闭哪个作家以及在哪里关闭感到困惑。

import java.net.*; 
import java.io.*;

public class URLReader {
    public static void main(String[] args) throws Exception {
        BufferedReader br = null;
        try {
            // change this value
            FileInputStream fis = new FileInputStream("C:\Users\Rao\Desktop\test.txt");
            br = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
            String sCurrentLine;
            while ((sCurrentLine = br.readLine()) != null) {
                processLine(sCurrentLine); //error
            }
        } finally {
            if (br != null)
                br.close();
        }
    }

    public static void processLine(String line) throws IOException {
        String prename = line.substring(22);
        int siz= prename.indexOf(":");
        String name = prename.substring(0, siz);

        URL oracle = new URL("http://ip-api.com/json/"+name);
        BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) // error
            // System.out.println(inputLine);
            in.close();  
        String baby = (line + "\t" + inputLine); 

        try {
            FileWriter writer = new FileWriter("C:\Users\Rao\Desktop\output.txt", true);
            writer.write(baby);
            writer.write("\r\n");   // write new line
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

The exception is as follows:

例外情况如下:

Exception in thread "main" java.io.IOException: Stream closed
    at java.io.BufferedReader.ensureOpen(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at URLReader.processLine(URLReader.java:31)
    at URLReader.main(URLReader.java:13)

采纳答案by Jens

You close the input stream in your loop:

您关闭循环中的输入流:

while ((inputLine = in.readLine()) != null) // error

               // System.out.println(inputLine);
in.close();  

You should close the stream outside of the loop:

您应该关闭循环外的流:

while ((inputLine = in.readLine()) != null) // error
{
   //dosomething
   // System.out.println(inputLine);
}
in.close();  

回答by morels

You should put a function call in the while loop, like:

您应该在 while 循环中放置一个函数调用,例如:

  1. a System.out.println("Hi, I'm a row!");or
  2. uncomment System.out.println(inputLine);or
  3. put a semicolon at the end of the while statement
  1. 一个System.out.println("Hi, I'm a row!");
  2. 取消注释System.out.println(inputLine);
  3. 在 while 语句的末尾放一个分号

in order to let it to execute properly.

为了让它正确执行。

The code as it is written executes (comments omitted):

编写的代码执行(省略注释):

...
   while ((inputLine = in.readLine()) != null)
     in.close();  
...

so the first cycle of the loop executes correctly and runs in.close(). Then the second cycle the call inputLine = in.readLine()fails because the stream is closed and then the exception is thrown.

所以循环的第一个循环正确执行并运行in.close()。然后第二个循环调用inputLine = in.readLine()失败,因为流关闭,然后抛出异常。