[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 300
  • Last Modified:

File Input. Reading In & Changing Variable & Adding Lines of Code

Hello folks :)

I have a program, and it contains a class which has a variable called number, so the line reads. Say that number means something to the program that makes something go 1 times. (not decided yet)

int number = 1;

I have a file called file.txt. What i'd like to be able to is put 'configuration' information in it.  
So the first line of the text file will say. I don't know if there is also anyway that the program knows to only process the first line of code

number = 54;

And .. what i'd like to happen 'at runtime' is that the value for number change to 54 with in program (again at runtime - no recompiles)
In otherwords the value for number be configured at runtime (kind of like input from the console but this time from the file)
Is that possible. Any suggestions.


SECOND
What if I wanted to add more lines in that txt file, that also lines of code.
So say the second line says.

System.out.println("This is today's quote of the day");

So somewhere in the code, it will say read the second line of code, insert here and run.

I assume we'd need some sort of method that reads, input from this file and then runs the block of code based on input. I assume it needs to process this as a string and paste it into the code on the fly.

I'm kinda stumped as I don't get how this would happen if the my source file hasn't re-compiled this data at runtime.

Much Thanks.
0
pauldhadialla
Asked:
pauldhadialla
  • 6
1 Solution
 
CEHJCommented:
You can treat a text file as a List of String. This class does just that. Let me know if you need help on how to use it:



import java.io.*;
import java.util.ArrayList;

/**
 *  A utility class the allows a text file to be
 *  treated as a collection of Strings
 *
 * @author     CEHJ
 * @created    29 February 2004
 */
public class StringList extends ArrayList {

  /**
   *Constructor for the StringList object
   */
  public StringList() {
    super();
  }

  public void read(InputStream in) {
    read(new InputStreamReader(in));
  }


  public void read(Reader r) {
    String line = null;
    BufferedReader in = null;
    try {
      in = new BufferedReader(r);
      while ((line = in.readLine()) != null) {
        add(line);
      }
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    finally {
      try {
        in.close();
      }
      catch (IOException e) {
        e.printStackTrace();
      }
    }

  }

  /**
   *  Constructor for the StringList object
   *
   * @param  fileName  The file to open
   */
  public void read(String fileName) {
    try {
      read(new FileReader(fileName));
    }
    catch(IOException e) {
      e.printStackTrace();
    }
  }

  /**
   *  Save the String to named file
   *
   * @param  fileName  The name of the file to save to
   */
  public void save(String fileName) {
    PrintWriter out = null;
    try {
      out = new PrintWriter(new FileOutputStream(fileName), true);
      for (int i = 0; i < size(); i++) {
        out.println((String) get(i));
      }
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    finally {
      if (out != null) {
        out.close();
      }
    }
  }

}


0
 
CEHJCommented:
For example:

StringList sl = new StringList();
sl.read("config.txt");

1. Get number of iterations (e.g. stored at line 3 in the file)

int numIterations = Integer.parseInt((String)sl.get(2));

2.

// get quote of the day (which has been arranged to be the last line)

String quoteOfDay = (String)sl.get(sl.size() - 1);
0
 
CEHJCommented:
But actually both of those things could be done with a simple Properties class:

Properties props = new Properties();
props.load(new FileInputStream("config.properties"));
int numIterations = Integer.parseInt(props.getProperty("iterations"));
String quoteOfDay = props.getProperty("quoteOfTheDay");
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CEHJCommented:
To add a new property:

props.setProperty("wordOfTheDay", "propinquity");
props.store(new FileOutputStream("config.properties"));
0
 
pauldhadiallaAuthor Commented:
CEHJ
This is very good my friend. Just a small question.

When I call the information from the arraylist by doing the get()
I get the "full line": so it will say

number = 54;

Is there a way I can I can get "just" the 54 (the value essentially). I know how to cut that out with unix - is there a method that can do that in java, i seached and couldn't find one.

Secondly. I would also like to invoke the creation of objects by pulling some of those Arraylist items form the text file.
One of the lines for example would say:

someClass(new ChangeColor(blue));

So say i have some classes ChangeColor with a contructor / method.

ChangeColor(string color){
does something
}

In the text file,
someClass(new ChangeColor(blue));
is the second item in the list so....as input i can pull that with the sl.get(1)

But that someClass up there would need to take sl.get(1) and run it so that the object gets created.
Then again I might have many lines in the text file trying to create different objects

someClass(new ChangeColor(blue));
someClass(new ChangeSize(333));
someClass(new ChangeTexture(rough));

So when i read that I'm really trying to create the object and pass it an argument but that 'someClass'
Is it possible to create a class that will just take that whole string someClass(new ChangeColor(blue));

I guess we would call that class it from main, and since its a treaded application - that class would just have action ( ) method with a list of items from the text file
being called.

Not sure if that is very clear.

Thanks kindly
0
 
CEHJCommented:
>>Is there a way I can I can get "just" the 54 (the value essentially).

You should use the second way i mentioned, using a Properties class

int number = Integer.parseInt(props.getProperty("number"));

and

ChangeColor cc = new ChangeColor(props.getProperty("color"));
0
 
CEHJCommented:
8-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now