Solved

Using ini files in Java

Posted on 2000-02-24
6
512 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
advertisement module in core php 4 160
java jdbc batch example 8 32
MySqlDump not dumping triggers 1 31
null output 3 25
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…
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…
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…
This video teaches viewers about errors in exception handling.

776 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