By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,969 Members | 2,250 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,969 IT Pros & Developers. It's quick & easy.

Reading a Properties file using BufferedInputStream

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: 6
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
Expert 100+
P: 197
...
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

Expert 10K+
P: 11,448
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
Expert 100+
P: 197
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

Expert 10K+
P: 11,448
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.