IOException:读取错误 [英] IOException: Read Error

查看:186
本文介绍了IOException:读取错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

嘿,我正在使用java.util.Properties读取ini文件;我遇到了一个奇怪的问题.当我尝试加载特定文件时,该东西吐出了我一直试图消除的奇怪异常.

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.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.

txt文件为:

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

如果我删除num_planes = 1行,该文件将被很好地读取.

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

相关代码:

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

}

推荐答案

这就是为什么:

您的代码(flight_optimizer.java,第82行及以后):

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

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

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

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

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

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

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.

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

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

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.

这篇关于IOException:读取错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆