wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes How to read properties with no key giving Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to read properties with no key giving" Watch "How to read properties with no key giving" New topic
Author

How to read properties with no key giving

Simpson Kumar
Ranch Hand

Joined: Mar 19, 2008
Posts: 261
I got a requirement which needs to read the config file (properties) and display the data accordingly. But that config file may change in future.
According to that changes we need to display.

We can read the file and display by passing the key to load the property, but here we can't guarantee what key and how many properties they are adding. Is there a way to load each property one by one sequentially?
I can read the properties by



But I can't get the keys line by line (sequentially). I need that way. could someone give me suggestion on this


Thanks,
Kumar
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

What do you mean you can't read the keys sequentially? You have Set a = props.keySet(); That gives you the list of keys. What else do you need?


GenRocket - Experts at Building Test Data
Simpson Kumar
Ranch Hand

Joined: Mar 19, 2008
Posts: 261
Hi Gregg,

Yes, I can't read properties sequentially, here I'm attaching the properties file and also result I got

Here is my properties file :


The java code to read properties sequentially


Im expecting the results should be displayed like the properties listed in the properties file. but I got the following out of order. This is console output
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Yep, you are correct. No order is guaranteed. If you need to preserve order you are going to have to write a parser yourself. Which really isn't that hard.



If you aren't using Java5 then use the BufferedInputStream.readLine() instead of the Scanner class.
Simpson Kumar
Ranch Hand

Joined: Mar 19, 2008
Posts: 261
Hi gregg,
Thanks for giving..

here I got a doubt.. what I'm supposed to put instead of System.getProperty("line.separator")
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41089
    
  44
Gregg Bolinger wrote:If you need to preserve order you are going to have to write a parser yourself. Which really isn't that hard.

Especially if there's work one can draw on :-)


Ping & DNS - my free Android networking tools app
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Sudhakar Kumar wrote:Hi gregg,
Thanks for giving..

here I got a doubt.. what I'm supposed to put instead of System.getProperty("line.separator")


Why would you put something instead of that? If using Scanner, that tells the scanner what to break elements on. You want it to break on a new line. If you aren't using Scanner, you don't need it at all.
Simpson Kumar
Ranch Hand

Joined: Mar 19, 2008
Posts: 261
Gregg Bolinger wrote:

Why would you put something instead of that? If using Scanner, that tells the scanner what to break elements on. You want it to break on a new line. If you aren't using Scanner, you don't need it at all.


I got read properties sequentially with Scanner, now I will implement that into my real code. let me try..

Thanks for your suggestion
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Are those really your field names? And will they always be "fieldX.readOnly" etc? If so, you can use the following technique:

This code will continue as long as the field for the index is there. You can't have any gaps, but the number of fields can be virtually unlimited.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Simpson Kumar
Ranch Hand

Joined: Mar 19, 2008
Posts: 261
Rob Prime wrote:Are those really your field names? And will they always be "fieldX.readOnly" etc? If so, you can use the following technique:

This code will continue as long as the field for the index is there. You can't have any gaps, but the number of fields can be virtually unlimited.



Hey Rob, I tried your logic too, I feel this one is good and easy and it works. Thanks

Now I need another clue, how to read updated properties file without rebooting the server?
Generally if we update the properties file, we need to reboot the server in order to load updated properties. Is there any approach to avoid rebooting?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Create a method that reads the new properties file and propagates the fields to all code that needs them, then call that method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to read properties with no key giving
 
Similar Threads
How to read instance variable dynamically?
load Properites from ByteArrayInputStream
Best Way of Accessing Constants
How to load Properties Dynamically at run time?
Need suggestion