69134

IOException: Read Error

Question:

If you want more info on the error, the full source can be downloaded <a href="http://www.filefactory.com/file/cbcd3ec/n/src.zip" rel="nofollow">here</a>

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.

the txt file is:

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.

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 }

Answer1:

That is why:

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); }

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(); }

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.

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.

Answer2:

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.

Recommend

  • Android Studio not working after update 1.3.0
  • Is it safe to cast void pointer to char pointer pointer
  • After Installing PMD in Luna in Prespective it is Not Showing?
  • NuGet package restore consent without NuGet
  • What's a good way to make a one-shot KVO observation?
  • chrome devtools inconsistency array length
  • Sticky Service not restarting after RAM full on Xiaomi / Huawei / Lava
  • Where to put clearQueue in jQuery code
  • How to avoid multiple definition of function (Linux, GCC/G++, Code::Blocks)
  • Each Radiobutton for each form or 1 Form for all radiobuttons?
  • DNS Lookup failed - Error with all browsers [closed]
  • Merge list of objects into consistent list based on common matching attribute in Python
  • Mapping two workspace folders to the same working directory
  • WooCommerce hook after order is updated?
  • Is mp4 stream able with ffserver?
  • Return to second to last URL in MVC (return View with previous filter conditions applied)?
  • Find longest path less than or equal to given value of an acyclic, directed graph in Python
  • Django model inheritance, filtering models
  • How can I set a binding to a Combox in a UserControl?
  • Does it make sense to call System.gc() and Thread.sleep() when working on Bitmaps?
  • Allowing both email and username for authentication
  • Get one-time binding to work for ng-if
  • Spring Data JPA custom method causing PropertyReferenceException
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • Problems to linebreak with an int in JLabel
  • Sony Xperia Z Tablet not found by adb
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Excel - Autoshape get it's name from cell (value)
  • Possible to stop flickering java tooltip in heavyweight mode?
  • output of program is not same as passed argument
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • How can I use Kendo UI with Razor?
  • How do you troubleshoot character encoding problems?
  • How can I estimate amount of memory left with calling System.gc()?
  • Akka Routing: Reply's send to router ends up as dead letters
  • Error creating VM instance in Google Compute Engine
  • Hits per day in Google Big Query
  • unknown Exception android
  • Checking variable from a different class in C#
  • Django query for large number of relationships