Solved

Using ini files in Java

Posted on 2000-02-24
6
517 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
[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
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Suggested Courses

732 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