Solved

Configuration Properties

Posted on 2007-04-02
7
257 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:Mayank S
Mayank S 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: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
 

Author Comment

by:komlaaa
ID: 18841177
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 90
github account with ecipse 1 47
print map entry 34 69
String array comparison 4 34
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This video teaches viewers about errors in exception handling.
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.

772 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