469,270 Members | 1,164 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

Reading a Properties file using BufferedInputStream

I'm trying to read a properties file using BufferedInputStream and getResourceAsStream(),but getting "java.io.IOException: Stream closed' exception.

My properties file is not set in the classpath, I even tried by incuding the file as a jar in the classpath but invain.

My code snippet is:

BufferedInputStream bufferedinputstream =new BufferedInputStream(getClass().getClassLoader().ge tResourceAsStream(s));

where 's' is the name of the properties file.

I'm running as a standalone program.

Can anyone help me out in resolving the issue.
Jul 18 '07 #1
5 11291
I'm trying to read a properties file using BufferedInputStream and getResourceAsStream(),but getting "java.io.IOException: Stream closed' exception.

My properties file is not set in the classpath, I even tried by incuding the file as a jar in the classpath but invain.

My code snippet is:

BufferedInputStream bufferedinputstream =new BufferedInputStream(getClass().getClassLoader().ge tResourceAsStream(s));

where 's' is the name of the properties file.

I'm running as a standalone program.

Can anyone help me out in resolving the issue.



The class that has the code u have given above must throw IOException
public class Test
{


public static void main (String Args []) throws IOException{


BufferedInputStream bis = new BufferedInputStream(new FileInputStream("mydata.properties"));
DataInputStream dis = new DataInputStream(bis);

try {
String st=dis.readLine();
if(st!=null){
System.out.println(st);
}

} catch (Exception e) {
e.printStackTrace();
}
}
This is a sample code that works properly.I have placed the property file in the root path itself.
Jul 18 '07 #2
prometheuzz
197 Expert 100+
...
This is a sample code that works properly.I have placed the property file in the root path itself.
No, that is not how you're supposed to read a Properties file. Here's how:

Expand|Select|Wrap|Line Numbers
  1.  FileInputStream fis = new FileInputStream("C:/Temp/test.properties");
  2. Properties props = new Properties();
  3. props.load(fis);
  4. fis.close();
  5.  
  6. // ...
  7.  
  8. System.out.println(props.get("key"));
@OP: Are you perhaps closing the stream before you call Properties' load(...) method?
Jul 18 '07 #3
JosAH
11,448 Expert 8TB
Using getResourceAsStream() isn't that wrong. It allows for streams to be
opened in jars etc. Just get the path to the resource right and that's it.
Specify the path to the resource either somewhere external or just relative to
where the class itself (the one that invokes the resourceAsStream() method)
is located. It works for me; all the time ;-)

kind regards,

Jos
Jul 18 '07 #4
prometheuzz
197 Expert 100+
Using getResourceAsStream() isn't that wrong. It allows for streams to be
opened in jars etc. Just get the path to the resource right and that's it.
Specify the path to the resource either somewhere external or just relative to
where the class itself (the one that invokes the resourceAsStream() method)
is located. It works for me; all the time ;-)

kind regards,

Jos
I was talking about the solution proposed in reply #2 where someone suggested to read the properties file line by line like an ordinary (text) file.
; )
Jul 18 '07 #5
JosAH
11,448 Expert 8TB
I was talking about the solution proposed in reply #2 where someone suggested to read the properties file line by line like an ordinary (text) file.
; )
Ah, yes; I missed that; sorry about that; reading the properties thing yourself is
certainly not the way to go; the Properties object does more than reading lines,
e.g. it also converts those \u unicode escape sequences. wheels and reinventions
and all that ;-)

kind regards,

Jos

ps. I removed some more noise words from your SV text; that size on disk is
down from ~ 3.5MB to ~ 3.1MB ;-)
Jul 18 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by syntax | last post: by
2 posts views Thread by Simon Verona | last post: by
3 posts views Thread by richard.kessler | last post: by
tshabza
2 posts views Thread by tshabza | last post: by
4 posts views Thread by tushar.saxena | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.