Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Properties Files

 
Joel Cochran
Ranch Hand
Posts: 301
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After many months away I am starting to get back to my Java project, which means I am relearning may things I thought I understood...
I have a program that performs a JDBC connection but I don't want to hardcode the IP address or Database drivers, I would like to be able to store several and simply 'activate' the one I'm currently using. If I remember right, I can put these parameters in something called a Properties file and access them at run time. This way, I only need top change the properties file in order to change databases, machines, etc.
So what are these things, how do I set one up, and how do I access one from a program?
Thanks,
Joel
 
Don Gardner
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think what you are looking for is a ResourceBundle. Sun has a tutorial on how to used resource bundles to create text properties files.
I hope this is helpful.
[This message has been edited by Don Gardner (edited October 25, 2001).]
 
Joel Cochran
Ranch Hand
Posts: 301
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the links.
It seems, though, that ResourceBundle is primarily concerned with Locales and language translation. They mention Properties files, which as I thought are simple text files, so I think this close to what I want. All I need to know now is how to access those files on the fly to retrieve Strings.
Joel
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like this:

Is this the bit you were looking for?
Dave
 
Joel Cochran
Ranch Hand
Posts: 301
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that looks MUCH more like what I had in mind! I just started learning some of the IO stuff yesterday (including how to create a new .jpg) and this goes hand in hand with what I was reading.
So can I have multiple properties in this file or would I need multiple files for different values, like on e file for IP address, one file for JDBC Driver name, etc.?
Also, I'm assuming that "/files/..." would have to be a sub-folder underneath the folder the program is running in (when creating the .jpg it automatically goes in the same folder as the program). What are the rules for specifying a target location?
Thanks for the help, it is most appreciated!
Joel
 
Paul Stevens
Ranch Hand
Posts: 2823
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I prefer to use relative paths. There are no real rules though. But remember if you can't insure that it is installed in an exact directory actual paths will fail.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the methods in the File class to see various ways of determining the path to a file.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Stevens:
I prefer to use relative paths. There are no real rules though. But remember if you can't insure that it is installed in an exact directory actual paths will fail.

I just hacked the above code to provide an example, I tend to use the ClassLoader to locate the file so that the properties become part of the package...

Dave
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic