Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Modified java.util.Properties class

 
Nick Kocher
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need something like java.util.Properties class, where the store() method writes 2 extra lines at the beginning of the file and the load() method reads this two lines and makes something with them. Should I extend the Properties class and override the load() and store() methods? how would then the code in this methods look like aproximately?(Is it possible at all to find the code for the classes of API?) Or is it better to create a new class with a HashTable() object and use the java.io.* package?
Thank you for any help!
 
Raj Chila
Ranch Hand
Posts: 128
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

you could extend the Properties class, and over ride the store and load methods...you can use the "super()" to automatically duplicate what the superClass method is doing...so you can over ride the method. add your lines to the output stream and then call super().

I dont think you need the source code, but it is freely available the the JDK download I think.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might find it cleaner to create a new class that has an instance of Properties. Your methods might look like:

This looks like more work because you have to provide a bunch of methods that do nothing but call the Properties instance, but there are some subtle dangers in extending any concrete class. Even though Properties is likely to be very stable over tme, this "decorator" is safer in the long run.

Of course a quick look at the doc shows Sun already stepped up to the dangers by extending HashMap. It's their problem to assure that future changes to HashMap don't break Properties. Don't join them in the risk if you can help it.


[ December 04, 2004: Message edited by: Stan James ]
 
Nick Kocher
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, thank you all!

the solution suggested here is exactly what I need!

I didn't understand about the source code to API classes, is it somewhere in JDK???
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic