Reading a Properties file using BufferedInputStream

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sajithamol

    Reading a Properties file using BufferedInputStream

    I'm trying to read a properties file using BufferedInputSt ream and getResourceAsSt ream(),but getting "java.io.IOExce ption: 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:

    BufferedInputSt ream bufferedinputst ream =new BufferedInputSt ream(getClass() .getClassLoader ().getResourceA sStream(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.
  • mailanusha
    New Member
    • Jun 2007
    • 6

    #2
    Originally posted by sajithamol
    I'm trying to read a properties file using BufferedInputSt ream and getResourceAsSt ream(),but getting "java.io.IOExce ption: 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:

    BufferedInputSt ream bufferedinputst ream =new BufferedInputSt ream(getClass() .getClassLoader ().getResourceA sStream(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{


    BufferedInputSt ream bis = new BufferedInputSt ream(new FileInputStream ("mydata.proper ties"));
    DataInputStream dis = new DataInputStream (bis);

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

    } catch (Exception e) {
    e.printStackTra ce();
    }
    }
    This is a sample code that works properly.I have placed the property file in the root path itself.

    Comment

    • prometheuzz
      Recognized Expert New Member
      • Apr 2007
      • 197

      #3
      Originally posted by mailanusha
      ...
      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:

      [CODE=java] FileInputStream fis = new FileInputStream ("C:/Temp/test.properties ");
      Properties props = new Properties();
      props.load(fis) ;
      fis.close();

      // ...

      System.out.prin tln(props.get(" key"));[/CODE]

      @OP: Are you perhaps closing the stream before you call Properties' load(...) method?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Using getResourceAsSt ream() 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 resourceAsStrea m() method)
        is located. It works for me; all the time ;-)

        kind regards,

        Jos

        Comment

        • prometheuzz
          Recognized Expert New Member
          • Apr 2007
          • 197

          #5
          Originally posted by JosAH
          Using getResourceAsSt ream() 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 resourceAsStrea m() 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.
          ; )

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by prometheuzz
            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 ;-)

            Comment

            Working...