Java Forum / First Aid / March 2006
File IO -- Reading Config Files Easily
Hal Vaughan - 30 Mar 2006 19:27 GMT I remember somewhere I saw a Java program that had a config file with name/value pairs like:
alpha=one beta=two gamma=three
The programmer was using something in the Java API to easily read this in and use the value pairs for setting variables, or maybe for setting values in something like a HashMap or Vector. I can't remember what he used or what it was called, but it was a basic shortcut from having to read the file line by line, split each line on the "=", and parse the results.
Any help on what would do this or what to call it?
Thanks!
Hal
geeker87@gmail.com - 30 Mar 2006 19:29 GMT Implementing that would simply be a case of opening a file, a few readLine()s and some string splitting. Would take 5 minutes to write -- why do you need to use an existing one?
Steve W. Jackson - 30 Mar 2006 21:44 GMT > Implementing that would simply be a case of opening a file, a few > readLine()s and some string splitting. > Would take 5 minutes to write -- why do you need to use an existing one? Because wasting 5 minutes to write one when the Properties class is already part of Java is a bit dumb, don't you think?
 Signature Steve W. Jackson Montgomery, Alabama
Roedy Green - 30 Mar 2006 23:00 GMT On Thu, 30 Mar 2006 14:44:10 -0600, "Steve W. Jackson" <stevewjackson@charter.net> wrote, quoted or indirectly quoted someone who said :
>Because wasting 5 minutes to write one when the Properties class is >already part of Java is a bit dumb, don't you think? Check out the strange quoting rules. You might change your mind.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Steve W. Jackson - 30 Mar 2006 23:20 GMT > On Thu, 30 Mar 2006 14:44:10 -0600, "Steve W. Jackson" > <stevewjackson@charter.net> wrote, quoted or indirectly quoted someone [quoted text clipped - 4 lines] > > Check out the strange quoting rules. You might change your mind. I admit those can be a little cumbersome if you really need some characters. But I've used Properties numerous times without bumping up against those rules. If I were using 1.5, it seems those could be readily handled by using XML, unless I miss my guess.
= Steve =
 Signature Steve W. Jackson Montgomery, Alabama
Hal Vaughan - 31 Mar 2006 02:39 GMT > Implementing that would simply be a case of opening a file, a few > readLine()s and some string splitting. > Would take 5 minutes to write -- why do you need to use an existing one? In high school I came up with a great idea for a play I dashed off in a few days. It had a ghost in it who came back to ask his son for revenge, a woman that went mad, a sword fight at the end where everyone ended up dead from poison on the blades and in wine and even a great line or two about being and not being.
I still can't figure out why I got an F on the play.
Why write something that's been written? And how many 5 minute blocks of my time do I waste doing what I don't have to? I'm more focused on developing the habit of NOT re-writing what's already there.
Hal
Monique Y. Mudama - 31 Mar 2006 04:30 GMT >> Implementing that would simply be a case of opening a file, a few >> readLine()s and some string splitting. Would take 5 minutes to >> write -- why do you need to use an existing one? [snip]
> Why write something that's been written? And how many 5 minute > blocks of my time do I waste doing what I don't have to? I'm more > focused on developing the habit of NOT re-writing what's already > there. The problem with spending 5 minutes rewriting an existing utility is that it's never really 5 minutes. Oh, sure, maybe it takes 5 minutes (I'm skeptical even on that part) to write the first draft. And maybe it even compiles and, at first blush, appears to work. But I guarantee that there will be bugs, and those bugs will take far more than 5 minutes to solve.
One strong reason for using libraries is that they've been tested, over and over, and are pretty robust. Sure, you can say, "Look, this class that's been around for 5+ years *still* has bugs!" -- but think of what this implies for your own code, which hasn't been refined for 5 years and hasn't been used by thousands of developers.
 Signature monique
Ask smart questions, get good answers: http://www.catb.org/~esr/faqs/smart-questions.html
Monique Y. Mudama - 30 Mar 2006 19:38 GMT > The programmer was using something in the Java API to easily read this in > and use the value pairs for setting variables, or maybe for setting values [quoted text clipped - 3 lines] > > Any help on what would do this or what to call it? I think you're talking about using a Properties file.
http://javaalmanac.com/egs/java.util/Props.html
 Signature monique
Ask smart questions, get good answers: http://www.catb.org/~esr/faqs/smart-questions.html
Eric Sosman - 30 Mar 2006 19:51 GMT Hal Vaughan wrote On 03/30/06 13:27,:
> I remember somewhere I saw a Java program that had a config file with > name/value pairs like: [quoted text clipped - 8 lines] > what it was called, but it was a basic shortcut from having to read the > file line by line, split each line on the "=", and parse the results. Sounds like java.util.prefs.Preferences.
 Signature Eric.Sosman@sun.com
Eric Sosman - 30 Mar 2006 19:58 GMT Eric Sosman wrote On 03/30/06 13:51,:
> Hal Vaughan wrote On 03/30/06 13:27,: > [quoted text clipped - 12 lines] > > Sounds like java.util.prefs.Preferences. No; wait; sorry: Preferences stores its name/value pairs in XML format, not in plain text. Monique Mudama is probably right: take a look at java.util.Properties.
 Signature Eric.Sosman@sun.com
Monique Y. Mudama - 30 Mar 2006 20:12 GMT > No; wait; sorry: Preferences stores its name/value pairs in XML > format, not in plain text. Monique Mudama is probably right: > take a look at java.util.Properties. I'm tickled by the idea that there might be enough (ie, more than one) Moniques around here that I need to be qualified with a last name =)
It would be great if there were enough regular female posters around here to necessitate it!
 Signature monique
Ask smart questions, get good answers: http://www.catb.org/~esr/faqs/smart-questions.html
Eric Sosman - 30 Mar 2006 20:29 GMT Monique Y. Mudama wrote On 03/30/06 14:12,:
>> No; wait; sorry: Preferences stores its name/value pairs in XML >> format, not in plain text. Monique Mudama is probably right: >> take a look at java.util.Properties. > > I'm tickled by the idea that there might be enough (ie, more than one) > Moniques around here that I need to be qualified with a last name =) Silly me, for worrying that omitting the "Y." might have been an inexcusable liberty ...
> It would be great if there were enough regular female posters around > here to necessitate it! Vive la différence!
 Signature Eric.Sosman@sun.com
Monique Y. Mudama - 30 Mar 2006 20:52 GMT >> I'm tickled by the idea that there might be enough (ie, more than >> one) Moniques around here that I need to be qualified with a last >> name =) > > Silly me, for worrying that omitting the "Y." might have been an > inexcusable liberty ... I just like the way it looks. Any resemblance to a preference for formality is purely coincidental and definitely misleading.
 Signature monique
Ask smart questions, get good answers: http://www.catb.org/~esr/faqs/smart-questions.html
Roedy Green - 30 Mar 2006 20:48 GMT On Thu, 30 Mar 2006 13:27:53 -0500, Hal Vaughan <hal@thresholddigital.com> wrote, quoted or indirectly quoted someone who said :
>Any help on what would do this or what to call it? see http://mindprod.com/jgloss/properties.html
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
jcsnippets.atspace.com - 30 Mar 2006 22:08 GMT > I remember somewhere I saw a Java program that had a config file with > name/value pairs like: [quoted text clipped - 10 lines] > > Any help on what would do this or what to call it? It's the Properties class - more information and sample code can be found in the following link.
http://jcsnippets.atspace.com/java/input-output/store-program-settings.html
Best regards,
JC -- http://jcsnippets.atspace.com/ a collection of source code, tips and tricks
Hal Vaughan - 31 Mar 2006 02:38 GMT > I remember somewhere I saw a Java program that had a config file with > name/value pairs like: [quoted text clipped - 10 lines] > > Any help on what would do this or what to call it? Thanks, everyone for the responses. Yes, it's java.util.properties, and thanks, Roedy, for the link. Your page has bailed me out many times and the effort to create it is much appreciated.
In this case, I couldn't remember a thing about what it was, so I wasn't even sure what to Google for. Config file? File Input? Settings? Values?
The help is appreciated!
Hal
Roedy Green - 31 Mar 2006 03:14 GMT On Thu, 30 Mar 2006 20:38:37 -0500, Hal Vaughan <hal@thresholddigital.com> wrote, quoted or indirectly quoted someone who said :
>Thanks, everyone for the responses. Yes, it's java.util.properties, and >thanks, Roedy, for the link. Your page has bailed me out many times and >the effort to create it is much appreciated. Ways you might have found it without help.
1. find a properties file that has the correct format and then look up "properties".
2. recall than you can feed in a key and get a value. This sounds like some sort of Hashtable or HashMap or at least a Map. Look up those classes and see if they have any subclasses.
3. recall the name of one of the system properties. Look that up which might have lead you to user properties.
The neural nets of the combined folks here are astounding at associating with minimal clues. Seems like a perfectly reasonable way to use the resource.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|