Solved

Using ini files in Java

Posted on 2000-02-24
6
509 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can I do a Massively massive community reviewing-and-grading site? 7 69
bitbucket vs gitbucket 3 55
Image decoding from Camera 3 72
map related example 6 29
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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
This video teaches viewers about errors in exception handling.

930 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now