This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Java Property File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Property File" Watch "Java Property File" New topic
Author

Java Property File

Francisco A Guimaraes
Ranch Hand

Joined: Mar 20, 2002
Posts: 182
How do I use a property file?
  • how do I write it?
  • where do I save it?
  • how do I use its properties inside a class?


  • I searched a bit in java.sun.com and the Java Tutorial but didn�t find anything on this topic.
    Thanks,
    Francisco


    Francisco<br />SCJP<br />please use the [code][/code] tags when showing code.Click <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">here</a> to see an example.
    Joel McNary
    Bartender

    Joined: Aug 20, 2001
    Posts: 1817

    Properties are passe; Preferences are the current rage. That said, properties are still useful for quick-and-easy settings.
    Basically, a Properties file contains key/value associations:
    ConnectionURL=jdbc dbc:MyDatabase
    DriveClass=sun.jdbc.odbc.JdbcOdbcDriver
    Password=pass_word
    UserName=web_user
    This is an example of a properties file specifying a Database connection.
    It is saved in the classpath with a .properties extension. (Say, as "MyDatabase.properties" in the "mypackage" package.)
    You use it by using the java.util.(Property)ResourceBundle class

    Of course, you can do other things with it as well. See the java.util.PropertyResourceBundle documentation for more information.


    Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
    Francisco A Guimaraes
    Ranch Hand

    Joined: Mar 20, 2002
    Posts: 182
    What�s the difference between a property and a preference? why is the last one better? Can you give an example?
    Francisco
    Joel McNary
    Bartender

    Joined: Aug 20, 2001
    Posts: 1817

    Unfortunately, I've not had the time to really get into the Preferences API, so my code still uses Properties. In the coming months I'll get around to making the switch (I've wrapped the Properties API anyway, so switching to Preferences should (hopefully) noe be that big a deal.)
    However, you can read Thomas Paul's newsletter article on the subject.
    [ May 27, 2003: Message edited by: Joel McNary ]
    Sudheera Liyanage
    Greenhorn

    Joined: Oct 14, 2002
    Posts: 7
    Hi,
    I am developing some java classes(APIs) which will be used by other developers in there applications.
    I use a property file to store application dependant values. ex:log file path name.
    I have put the .propertie file to same folder where my class file exists. In command prompt
    my class is running without problem. But when I use my IDE (Eclipse 2), it does not work(it cannot fine the file)).
    I have added the properties file to system classpath, it also not work.
    Please help me.
    Sudheera
    [ May 27, 2003: Message edited by: Sudheera Liyanage ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Java Property File
     
    Similar Threads
    Specify custom logging.properties file in the a Java program.
    Replace with Properties
    multiple struts-config files
    where to keep data
    log4j language problem