File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Properties Files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Properties Files" Watch "Properties Files" New topic
Author

Properties Files

Joel Cochran
Ranch Hand

Joined: Mar 23, 2001
Posts: 301
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


Wait a minute, I'm trying to think of something clever to say...<p>Joel
Don Gardner
Greenhorn

Joined: Oct 09, 2001
Posts: 25
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

Joined: Mar 23, 2001
Posts: 301
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

Joined: Mar 06, 2001
Posts: 13459

Like this:

Is this the bit you were looking for?
Dave
Joel Cochran
Ranch Hand

Joined: Mar 23, 2001
Posts: 301
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

Joined: May 17, 2001
Posts: 2823
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

Joined: Jul 22, 2000
Posts: 9043
    
  10
Check out the methods in the File class to see various ways of determining the path to a file.

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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
 
subject: Properties Files
 
Similar Threads
Email all logged messages
Database connection
Starting Java programs from different command windows
Exception while accessing .properties file.
How to access properties file from WEB-INF/ directory