The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
October 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

How to parse configuration information from a text file

Posted by Eduardo Werneck on October 22, 2001 at 9:57 AM

I'm trying to call a text file, using this ReadText class, inside another class (Restart), in order to replace the lines in this class. Unfortunately, I'm new in the language and I cannot figure out how to do that. Does anybody have any hint on how to do that?
The ReadText is the class that reads the text file. The GrenhouseControls has a class called Restart, where there is a copy of this text file. Actually, if we uncomment this text, it should be the configuration for the program.
At the end, there is the class Event that is missing to complete the package.
Thanks!
Eduardo
import java.io.*;
import java.util.*;

public class ReadText {
static LinkedList list;
static LinkedList list1;

public ReadText(){}


---------------- Here is the class that will receive the text.---
---------------- Scroll until the Restart class -------------
package tme2;

import tme2.*;
import java.io.*;
import java.util.*;

public class GreenhouseControls
extends Controller {
private boolean light = false;
private boolean water = false;
private boolean fans = false; // including boolean variable to track the state of the fans
private String thermostat = "Day";
private class FansOn extends Event { // inner class added to turn on the fans
public FansOn(long eventTime) {
super(eventTime);
}
public void action() {
// Put hardware control code here to
// physically turn on the fans.
fans = true;
}
public String description() {
return "Fan are on";
}
}
private class FansOff extends Event { //added inner class to turn off the fans
public FansOff(long eventTime) {
super(eventTime);
}
public void action() {
// Put hardware control code here to
// physically turn off the fans.
fans = false;
}
public String description() {
return "Fans are off";
}
}
private class LightOn extends Event {
public LightOn(long eventTime) {
super(eventTime);
}
public void action() {
// Put hardware control code here to
// physically turn on the light.
light = true;
}
public String description() {
return "Light is on";
}
}
private class LightOff extends Event {
public LightOff(long eventTime) {
super(eventTime);
}
public void action() {
// Put hardware control code here to
// physically turn off the light.
light = false;
}
public String description() {
return "Light is off";
}
}
private class WaterOn extends Event {
public WaterOn(long eventTime) {
super(eventTime);
}
public void action() {
// Put hardware control code here
water = true;
}
public String description() {
return "Greenhouse water is on";
}
}
private class WaterOff extends Event {
public WaterOff(long eventTime) {
super(eventTime);
}
public void action() {
// Put hardware control code here
water = false;
}
public String description() {
return "Greenhouse water is off";
}
}
private class ThermostatNight extends Event {
public ThermostatNight(long eventTime) {
super(eventTime);
}
public void action() {
// Put hardware control code here
thermostat = "Night";
}
public String description() {
return "Thermostat on night setting";
}
}
private class ThermostatDay extends Event {
public ThermostatDay(long eventTime) {
super(eventTime);
}
public void action() {
// Put hardware control code here
thermostat = "Day";
}
public String description() {
return "Thermostat on day setting";
}
}
// An example of an action() that inserts a
// new one of itself into the event list:
private int rings;
private class Bell extends Event {
public Bell(long eventTime) {
super(eventTime);
}
public void action() {
// Ring every 2 seconds, 'rings' times:
System.out.println("Bing!");
if(--rings > 0)
addEvent(new Bell(
System.currentTimeMillis() + 2000));
}
public String description() {
return "Ring bell";
}
}
private class Restart extends Event {
public Restart(long eventTime) {
super(eventTime);
}
public void action() {
long tm = System.currentTimeMillis();

// parse configuration information from a text file here:
// the text in comments below is what the text file contains.
/*

rings = 5;
addEvent(new ThermostatNight(tm));
addEvent(new LightOn(tm + 1000));
addEvent(new LightOff(tm + 2000));
addEvent(new FansOn(tm + 5000));
addEvent(new FansOff(tm + 6000));
addEvent(new WaterOn(tm + 3000));
addEvent(new WaterOff(tm + 8000));
addEvent(new Bell(tm + 9000));
addEvent(new ThermostatDay(tm + 10000));
*/
// Can even add a Restart object!
addEvent(new Restart(tm + 20000));
}
public String description() {
return "Restarting system";
}
}
public static void main(String[] args) {
GreenhouseControls gc =
new GreenhouseControls();
long tm = System.currentTimeMillis();
gc.addEvent(gc.new Restart(tm));
gc.run();
}
} ///:~

public static void PrintText (DataInputStream in) {
try {
list = new LinkedList();
String s;
while((s = in.readLine()) != null)
list.addFirst(s);
for(int i = 0; i < list.size(); i++)
// System.(

System.out.println(list.get(i));
} catch(FileNotFoundException e) {
System.err.println("File not found");
} catch(IOException e) {
System.err.println("IO error");
}
}

public LinkedList VText () {
list1 = new LinkedList(list);
return list1;
}
public static void main(String args[]) throws IOException {
try{


DataInputStream in =
new DataInputStream(
new BufferedInputStream(
new FileInputStream("examples.txt")));


PrintText(in);



} catch(FileNotFoundException e) {
System.err.println("File not found");
} catch(IOException e) {
System.err.println("IO error");
}
}
}
-------------------- Now the class Event ----------------------
package controller;

abstract public class Event {
private long evtTime;
public Event(long eventTime) {
evtTime = eventTime;
}
public boolean ready() {
return System.currentTimeMillis() >= evtTime;
}
abstract public void action();
abstract public String description();
} ///:~



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us