reading file information from a .ini file

I have a .ini file on the c:\ drive called offline.ini. it only has the parameter d:\javaproj\javalab\scan.txt in it. I need to be able to read this .ini file, get that parameter and pass the information to my program, instead of giving the program the direct directions to the scan.txt file. See the below code -The code works perfectly as written but for certification purposes I need to house the scan.txt information in an .ini file, so that if the directory where the file is housed is changed we will not have to edit the code but can just edit the .ini file. Please help -- this if my 1st java project and time is running out for me. I only have 2 duke dollars left. If you have any suggestions please feel free to change the code to give me an example.

//-Buffalo Offline Scan Program

//This program will allow the Buffalo user to continue scanning cases on pallets

//when the AS\400 is down. The scans will be sent to a flat file that will be

//FTPed to the AS\400 when it is back up and update the proper files.

//Program Authors: Dean Smith and Susan Riggin

//

package javalab;

import javabook.*;

import javalab.*;

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

import java.io.*;

import java.io.File.*;

import java.util.*;

import java.lang.reflect.*;

public class BuffOff1 extends Applet implements ActionListener

{

//-

//Data Members

//-

//Variables

private int scanCount;

private int sessionCount;

private String scanFilePath;

private String parms;

//Labels

private Label buffaloLabel = new Label();

private Label scanLabel = new Label();

private Label cPalletLabel = new Label();

private Label cCountLabel = new Label();

private Label tPalletLabel = new Label();

private Label tCountLabel = new Label();

private Label rButtonLabel = new Label();

private Label eButtonLabel = new Label();

//TextFields

private TextField scanTextField = new TextField(24);

//Buttons

private Button rButton = new Button("Reset");

private Button eButton = new Button("Exit");

//Text Areas

private TextArea cTextArea = new TextArea( 10, 40);

private TextArea tTextArea = new TextArea(10, 40);

//

//Constructor

//

public BuffOff1()

{

//Attach the GUI objects so they will appear on the screen.

setLayout (null);

buffaloLabel.setBounds(0, 6, 644, 27);

buffaloLabel.setFont(new Font ("dialog",Font.BOLD, 18));

buffaloLabel.setAlignment(Label.CENTER);

buffaloLabel.setText("Buffalo OffLine Scan");

cPalletLabel.setBounds(18, 60, 291, 23);

cPalletLabel.setFont(new Font ("dialog", Font.BOLD, 14));

cPalletLabel.setAlignment(Label.CENTER);

cPalletLabel.setText("Current Pallet");

cPalletLabel.setBackground(Color.cyan);

tPalletLabel.setBounds(322, 62, 291, 23);

tPalletLabel.setFont(new Font ("dialog", Font.BOLD, 14));

tPalletLabel.setAlignment(Label.CENTER);

tPalletLabel.setText("Total Pallets");

tPalletLabel.setBackground(Color.pink);

rButton.setBounds(129, 485, 56, 23);

rButton.setBackground(Color.cyan);

rButton.setLabel("Reset");

eButton.setBounds(459, 485, 56, 23);

eButton.setBackground(Color.pink);

eButton.setLabel("Exit");

cCountLabel.setBounds(18, 88, 291, 23);

cCountLabel.setFont(new Font("dialog", Font.BOLD, 12));

cCountLabel.setAlignment(Label.CENTER);

cCountLabel.setText("Current Pallet Case Count = " + scanCount);

cCountLabel.setBackground(Color.lightGray);

tCountLabel.setBounds(322, 88, 291, 23);

tCountLabel.setFont(new Font("dialog", Font.BOLD, 12));

tCountLabel.setAlignment(Label.CENTER);

tCountLabel.setText("Total Pallet Case Count = " + sessionCount);

tCountLabel.setBackground(Color.lightGray);

scanLabel.setBounds(18, 33, 160, 23);

scanLabel.setFont(new Font("dialog", Font.BOLD, 14));

scanLabel.setAlignment(Label.CENTER);

scanLabel.setText(" Current Barcode Scan: ");

scanTextField.setBounds(203, 34, 309, 23);

scanTextField.setBackground(Color.white);

eButtonLabel.setBounds(322, 460, 291, 23);

eButtonLabel.setAlignment(Label.CENTER);

eButtonLabel.setBackground(Color.pink);

eButtonLabel.setText("Press Exit to end Program.");

rButtonLabel.setBounds(18, 460, 291, 23);

rButtonLabel.setAlignment(Label.CENTER);

rButtonLabel.setBackground(Color.cyan);

rButtonLabel.setText("Press Reset for next pallet scan.");

cTextArea.setBounds(18, 118, 291, 333);

cTextArea.setBackground(Color.cyan);

tTextArea.setBounds(322, 118, 291, 333);

tTextArea.setBackground(Color.pink);

//Place the GUI objects on the applet.

add(buffaloLabel);

add(scanLabel);

add(cPalletLabel);

add(cCountLabel);

add(tCountLabel);

add(tPalletLabel);

add(cCountLabel);

add(rButtonLabel);

add(eButtonLabel);

add(scanTextField);

add(rButton);

add(eButton);

add(cTextArea);

add(tTextArea);

//Add applet as an action listener.

scanTextField.addActionListener(this);

rButton.addActionListener(this);

eButton.addActionListener(this);

}

//--

//Methods that make the program work

//--

//--

//method for action performed and action event

//--

public void actionPerformed(ActionEvent e)

{

if (e.getSource() == eButton) exit();

if (e.getSource() == scanTextField)

try {

scan();

}

catch(IOException f){}

if (e.getSource() == rButton) reset();

}

//

//-method for pressing the exit button

//

private void exit()

{

System.exit(0);

}

//

//method for pressing the reset button

//

private void reset()

{

scanCount = 0;

cTextArea.setText("");

cCountLabel.setText("Current Pallet Case Count = " + scanCount);

scanTextField.requestFocus();

}

//

//method for scanning barcode

//

private void scan() throws FileNotFoundException

{

String scanText = scanTextField.getText();

File scan = new File ("d:\\\\javaproj\\javalab\\scan.txt");

int c;

//Add to counts

++scanCount;

++sessionCount;

//Append scanned data to text areas

cTextArea.append(scanCount + "->" + scanText + " ");

tTextArea.append(sessionCount + "->" + scanText + " ");

tCountLabel.setText("Total Pallet Case Count = " + sessionCount);

cCountLabel.setText("Current Pallet Case Count = " + scanCount);

cTextArea.append("\r\n");

tTextArea.append("\r\n");

//Append scanned data directly to flat file.

try

{

FileWriter outputFile = new FileWriter("d:\\\\javaproj\\javalab\\scan.txt", true);

outputFile.write(scanTextField.getText());

outputFile.write("\r\n");

outputFile.close();

}

catch (IOException e)

{

}

//clear the scan field

scanTextField.setText("");

// position the cursor

scanTextField.requestFocus();

}

}

[7454 byte] By [riggisl] at [2007-9-26 3:33:23]
# 1

Check out java.util.Properties

You can create a FileInputStream instance corresponding to your ini file using new FileInputStream(String fullyQualifiedPathTo_ini_File), then given that instance to java.util.Properties.load(), then access the property value using java.util.Properties.getProperty("scan.txt.location")

In the .ini file you can have...

scan.txt.location=d:\\\\javaproj\\javalab\\scan.txt

neville_sequeira at 2007-6-29 12:01:50 > top of Java-index,Archived Forums,Java Programming...
# 2

Hi Susan! Neville's suggestion is right. Just remember that in order for the Java app to locate it, a properties file must live in a directory included in the classpath. This gives you more flexibility, i.e. should the location of the file be changed, then you need to make a corresponding change to the classpath. Hence, no change is required to the app code.

If changing the classpath is an issue, then use indirection, i.e. the properties file will contain the actual pathname of the file which contains your data. In this way, even if the actual data file is moved, you will need to change the contents of the first property file, w/o touching the code or the classpath.

However, you would be better off using properties, since the Java API (Properties and ResourceBundle classes) offer ready-made solutions to access data from properties files. I would recommend that any day over plain vanilla text files, where the programmer must implement the reading and parsing code.

Hope this helps! Feel free to ask if something's not too clear.

Cheers!

amolk at 2007-6-29 12:01:51 > top of Java-index,Archived Forums,Java Programming...