Solved

Configuration Properties

Posted on 2007-04-02
7
254 Views
Last Modified: 2008-03-10
Hi,
I have a Configuration Properties file that contains (variable, value) pairs. I would like to read those pairs from the file and use them to do database connection. Can someone show me a model

==== e.g Content of the file =======
password mypass
username myusername

============= in Addition ==========
i know how i can load the file content, but not how i pick a pair from it.

 // Read properties file.
    Properties properties = new Properties();
    try {
        properties.load(new FileInputStream("filename.properties"));
    } catch (IOException e) {
    }
   
 
0
Comment
Question by:komlaaa
  • 3
7 Comments
 
LVL 10

Accepted Solution

by:
ADSLMark earned 150 total points
ID: 18838702
You mean:

import java.io.*;
import java.util.*;

public class Example
{
    public static void main(String[] args)
    {
        Properties properties = new Properties();

        try
        {
            properties.load(new FileInputStream("config.txt"));
            System.out.println(properties.getProperty("username"));
            System.out.println(properties.getProperty("password"));
        }
        catch (IOException ioe)
        {
            System.err.println(ioe);
        }
    }
}

Simply use getProperty(String). Check http://java.sun.com/j2se/1.5.0/docs/api/java/util/Properties.html for more available methods.

Mark
0
 
LVL 30

Assisted Solution

by:mayankeagle
mayankeagle earned 150 total points
ID: 18839109
>>  but not how i pick a pair from it.

Use getProperty () as Mark has shown. Also close the file input stream in a finally block:

>> properties.load(new FileInputStream("filename.properties"));

FileInputStream fis = null ;
try
{
  fis = new FileInputStream ( "filename.properties" ) ;
  properties.load ( fis ) ;
} catch ( Exception e ) { e.printStackTrace () ; }
finally
{
  if ( fis != null )
    fis.close () ;
}
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 18839117
One of the best places to look for examples is Example depot:

http://www.exampledepot.com/egs/java.util/Props.html
0
 

Author Comment

by:komlaaa
ID: 18841177
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 19033974
Yes but that's only for the environmental properties. You can use the code posted above for any general properties file.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
allStar challenge 1 43
bitbucket vs gitbucket 3 35
Error with Java/Cache JDBC Classpath 2 21
backtracking recursion  code 19 41
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now