java IOException:读取错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5684250/
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-10-30 12:17:50  来源:igfitidea点击:

IOException: Read Error

javaioexception

提问by Ignoreme

If you want more info on the error, the full source can be downloaded here

如果您想了解有关错误的更多信息,可以在此处下载完整源代码

Hey, I'm reading an ini file using java.util.Properties; and I've run into a strange issue. When I try to load a specific file, the thing spits out this strange exception that I've been trying for about a day to eliminate.

嘿,我正在使用 java.util.Properties 读取 ini 文件;我遇到了一个奇怪的问题。当我尝试加载特定文件时,它会吐出这个奇怪的异常,我已经尝试了大约一天来消除它。

java.io.IOException: Read error
    at java.io.FileInputStream.readBytes(Native Method)
    at java.io.FileInputStream.read(Unknown Source)
    at java.util.Properties$LineReader.readLine(Unknown Source)
    at java.util.Properties.load0(Unknown Source)
    at java.util.Properties.load(Unknown Source)
    at IniReader.load(IniReader.java:20)
    at plane.<init>(plane.java:22)
    at renderingArea.<init>(flight_optimizer.java:93)
    at flight_optimizer_GUI.<init>(flight_optimizer.java:159)
    at flight_optimizer.main(flight_optimizer.java:46)

I had previously been reading this file just fine with no problems, I then changed a bit of how I was calling and had to add an extra line at the bottom. If I remove that line, the problem does not occour.

我之前一直在阅读这个文件,没有任何问题,然后我改变了我的调用方式,不得不在底部添加额外的一行。如果我删除那条线,问题就不会发生。

the txt file is:

txt文件是:

x=0
y=0
max_velocity=.1
passengers=100
num_planes=1

If I remove the num_planes=1 line, the file gets read fine.

如果我删除 num_planes=1 行,则文件读取正常。

Relevant code:

相关代码:

import java.util.Enumeration;

public class IniReader {

    //global vars

    public IniReader(){
        // initializeing stuffs
    }

    public void load(InputStream inStream) throws IOException {
        this.inStream = inStream;
        this.properties.load(this.inStream);
        this.keys = this.properties.propertyNames();
        inStream.close();
    }
}

class renderingArea extends JPanel {

    //Global vars
    private IniReader ini;

    public renderingArea(){
        super();
        // Initializing some things 
        files = new fileManager();
        ini = new IniReader();

        FileInputStream planeStream;
        FileInputStream cityStream;
        try {
            planeStream = files.getIni("plane.ini");
            ini.load(planeStream);

            //extraneous code

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        } catch (NumberFormatException e1) {
            e1.printStackTrace();
        }   
    }

    //moar extraneous code

}

回答by Alex Abdugafarov

That is why:

因此:

Your code (flight_optimizer.java, line 82 and further):

您的代码(flight_optimizer.java,第 82 行及更多):

FileInputStream planeStream;
...
planeStream = files.getIni("plane.ini");
ini.load(planeStream);
...
for( int i=0; i<planes.length; i++ ){
    planes[i] = new plane(planeStream);
}

Both the second line and every cycle iteration leads us here (IniReader.java, line 17):

第二行和每个循环迭代都将我们带到这里(IniReader.java,第 17 行):

public void load(InputStream inStream) throws IOException {
    this.inStream = inStream;
    this.properties.load(this.inStream);
    this.keys = this.properties.propertyNames();
    inStream.close();
}

You are trying to use the same InputStream multiple times, moreover, you are trying to use it after it already was closed. You will need to recreate the stream, or, preferably, read configuration once and use it multiple times.

您尝试多次使用相同的 InputStream,而且,您尝试在它已经关闭后使用它。您将需要重新创建流,或者最好是读取一次配置并多次使用它。

As a side note, the recommended way to use the streams in Java is the following:

作为旁注,在 Java 中使用流的推荐方法如下:

InputStream is = ...;
try {
   // Reading from the stream
} finally {
   is.close();
}

This will make sure that the system resources associated with the stream will always be released.

这将确保与流关联的系统资源将始终被释放。

回答by user1774051

I had the same problem. Turns out that my underlying InputStream was already closed. That became obvious when I ran my test under Linux, where a more meaningful error message was emitted by the operating system.

我有同样的问题。原来我的底层 InputStream 已经关闭。当我在 Linux 下运行我的测试时,这变得很明显,其中操作系统发出了更有意义的错误消息。