Solved

Using ini files in Java

Posted on 2000-02-24
6
503 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

706 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

20 Experts available now in Live!

Get 1:1 Help Now