Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Configuration Properties

Posted on 2007-04-02
7
Medium Priority
?
261 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
7 Comments
 
LVL 10

Accepted Solution

by:
ADSLMark earned 600 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:Mayank S
Mayank S earned 600 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:Mayank S
ID: 18839117
One of the best places to look for examples is Example depot:

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

Expert Comment

by:Mayank S
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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

722 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