Solved

Configuration Properties

Posted on 2007-04-02
7
258 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
custom annotations 9 42
login jsp example 24 64
How  Can  I  resolve   this  Error "java.sql.SQLException: Invalid column index" 4 49
Java class and jar 3 20
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

839 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