Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

"java.io.IOException: Stream closed" exception while reading properties

796745Apr 7 2012 — edited Apr 9 2012
Hi,

I am trying to read a property file using a sample code as below.

The property file is packaged as a resource in the same jar. Problem is that I am getting a java.io.IOException: Stream closed on "props.load" with Java 6. Strangely, the same piece of code works properly with Java 5.
package test;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

/**
 * This class is used to read the values from the property files
 */
final public class PropertyReader {

	private static final String FILE_NAME="test.properties";
	private static PropertyReader instance = null;
        private java.util.logging.Logger Logger = java.util.logging.Logger.getLogger(this.getClass().getPackage().getName());
	
	static
	{
		instance = new PropertyReader();
	}
	
	private PropertyReader() {}
	
	public static PropertyReader getInstance()
	{
		return instance;
	}
	
	/**
	 * This method Reads the property file and returns
	 * the property object.
	 * @return Property object.
	 */
	public synchronized Properties getProperties() {

		Properties prop = null;
		InputStream propReadStream = null;
		
		try {
			propReadStream = new BufferedInputStream(this.getClass().getResourceAsStream("/" + FILE_NAME));
			Logger.info(this.getClass().getName(), methodName, "buffer input is >>.."+propReadStream.toString());
			prop = new Properties();
			prop.load(propReadStream);
			Logger.info(this.getClass().getName(), methodName, "prop loading is done>>.." + prop.toString());
					
		} catch (final IOException e) {
			Logger.error(this.getClass().getName(), methodName, "IOException received >>>>>:"+e.getMessage());
		}catch(final Exception e)
		{
			Logger.error(this.getClass().getName(), methodName, "Exception received. Type:"+e.getClass().getName()+" Message:"+e.getMessage());
		}
		finally {
			if (propReadStream != null) {
				try{
					propReadStream.close();
				}
				catch(IOException e){
					Logger.error(this.getClass().getName(), methodName, "Exception received. Type:"+e.getClass().getName()+" Message:"+e.getMessage());
				}
			}
		}
		
		return prop;
	}
}
Could someone please help in identifying the problem here?

Thanks in advance.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 7 2012
Added on Apr 7 2012
5 comments
3,744 views