Reading Line from Text File
Hello guys
I have class that i've created with a variable called number
In the class,number = 1
I have a text file, where the first line of it reads
number = 2
(this is like configuration information for the class variable)
Is there anyway I can read this file in at runtime , it reads the value for number and the makes
number = 2.
I guess my question then is , how would you at runtime make some read a file and somehow change a value of a static variable.
Can data be read from a file and executed too.
Like can a method - doSomething(new doThis(90));
Be executed at runtime by reading a file.
So basically if I wanted different things done - rather than going into the file and recompiling it - methods are read from a file at runtime and depending on whats in the file - those methods run. I guess the same applies to my number = 2 issue.
Hope that makes sense
Thanks kindly
[972 byte] By [
eiffel65] at [2007-9-30 22:10:08]

Hi YAT, thank you for your reply
I am able to read the file from disk ok, but the thing is I don't get how to handle each line.
The lines would be delimited by semi colons for example
But i'd like to be able to take them and be able to apply / run them at runtime.
So i execute, and it reads a line from the text file that says
System.out.println("Wow");
and runs it - is that possible ?
> Hi YAT, thank you for your reply
>
> I am able to read the file from disk ok, but the thing
> is I don't get how to handle each line.
> The lines would be delimited by semi colons for
> example
>
> But i'd like to be able to take them and be able to
> apply / run them at runtime.
>
> So i execute, and it reads a line from the text file
> that says
>
> System.out.println("Wow");
> and runs it - is that possible ?
A few points:
1. Very, very risky. This means that if anyone modifies that file, they can cause your program to do all sorts of malicious things, including planting viruses, deleting important files, etc. etc. etc!
2. YAT, is reflection really necessary?
Why not just use a Properties file? Read in the properties, and set the variables as per the properties!
lutha
> methods are read from a file at runtime and depending
> on whats in the file - those methods run.
In that case the people producing the file you read basically is writing a Java program calling methods defined by you. Why don't you just collect your methods in a library and hand it over to people who want to use them in their program?