Solved

Using ini files in Java

Posted on 2000-02-24
6
514 Views
Last Modified: 2008-03-04
I want my program to read a ini file when it starts up. It looks like this :

[Settings #1]
connection=192.13.5.4
Port=4658

[Settings #2]
conneciton=198.15.4.86
port=5844

I want variables to get information from the ini file.

well you get the picture. Is there any easy way to do this ?

0
Comment
Question by:yngvi
6 Comments
 
LVL 16

Expert Comment

by:heyhey_
ID: 2553847
if you don't need sections
>> [Settings #1]


you can use java.util.Properties
0
 
LVL 5

Expert Comment

by:mbormann
ID: 2553911
else use manual parsing,

heyhey,
ur Email ID has changed?  Got a bouncer now.
wanted to send a nice link to you,send me a mail at my profile account.
0
 
LVL 4

Expert Comment

by:jerch
ID: 2553936
1Define a property file which contains
connection=198.15.4.86
port=5844

and save it to any filename (e.g. app.properties)

and to retrieve the value. Do this

Properties props = new Properties()
FileInputStream fis = new FileInputStream("app.properties");
        props.load(fis);
String connection = props.getProperty("connection");
String port = props.getProperty("port");

0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 16

Expert Comment

by:heyhey_
ID: 2554491
heyhey_@iname.com is working - I receive a lot of e-mails there.

heyhey@nettaxi.com is another option.
0
 
LVL 7

Expert Comment

by:faster
ID: 2556505
I wrote a class to read ini file, if you want I can send to you.
0
 
LVL 1

Accepted Solution

by:
jadrek85 earned 200 total points
ID: 2556924
Hi,
Here is the code which reads the connection and port from the ini file and displaying it on JLables. Copy this code and execute. It works as per your need. I have mentioned System.ini file as input/search file.
 place [Settings #1]
           connection=192.13.5.4
           Port=4658

           [Settings #2]
            conneciton=198.15.4.86
            port=5844  in your ini file and execute this code.

//---Start of code
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.io.*;

public class Frame2 extends JFrame {

  JLabel jLabel1 = new JLabel();
  JLabel jLabel2 = new JLabel();

  public Frame2() {
    try  {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
    Frame2 frame21 = new Frame2();
    frame21.setSize(400,400);
    frame21.setVisible(true);
  }

  private void jbInit() throws Exception {
    this.getContentPane().setLayout(null);
    jLabel1.setFont(new java.awt.Font("SansSerif", 0, 18));
    jLabel1.setBorder(BorderFactory.createEtchedBorder());
    jLabel1.setBounds(new Rectangle(11, 16, 362, 49));
    jLabel1.setText("Connection=");
    jLabel2.setText("Port=");
    //---- Start File read code
    File oFile=null;
    try{
      oFile = new File("d:/jad/system.ini");  // specify the path from the root
      if(oFile.exists()){
      System.out.println("Found ini File");
        LineNumberReader oLineReader = new LineNumberReader(new BufferedReader
                                       (new FileReader(oFile)));

      String sLine = null;
      String sWord = null;
      String sConn= null;
      String sPort=null;
        while((sLine = oLineReader.readLine())!=null) {
          System.out.println("In side While");
          sLine = sLine.trim();
          int iIndex = sLine.indexOf("=");
          if(iIndex != -1) {
            sWord = sLine.substring(0,iIndex);
            if(sWord.compareTo("connection") == 0) {
              sConn = sLine.substring(iIndex+1).trim();
              if(sConn.equals(""))
                sConn="Connection is not Specified";
                jLabel1.setText(jLabel1.getText()+sConn);
            }
            else if(sWord.compareTo("port") == 0) {
                    sPort = sLine.substring(iIndex+1).trim();
                    if(sPort.equals(""))
                      sPort="Port is not Specified";
                      jLabel2.setText(jLabel2.getText()+sPort);
              }
      }
    }
     oLineReader.close();
    }else
        jLabel1.setText("File not exist");

    }catch(Exception oExp){
          System.out.println("Error   "+oExp.toString());
     }
    //----- End File read code
    jLabel2.setFont(new java.awt.Font("SansSerif", 0, 18));
    jLabel2.setBorder(BorderFactory.createEtchedBorder());
    jLabel2.setBounds(new Rectangle(12, 105, 356, 51));
    this.getContentPane().add(jLabel1, null);
    this.getContentPane().add(jLabel2, null);
  }
}
//--- End of code

Cheers,
Jadhav
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

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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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:
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.

820 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