Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to read properties with no key giving

 
Simpson Kumar
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Simpson Kumar
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 261
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi gregg,
Thanks for giving..

here I got a doubt.. what I'm supposed to put instead of System.getProperty("line.separator")
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 :-)
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 261
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Simpson Kumar
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a method that reads the new properties file and propagates the fields to all code that needs them, then call that method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic