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]
# 1

You can read a file with FileInputStream or FileReader: the latter is more appropriate for text files. You can then parse each line as a string and a value, and use reflection to set the field named by the string, but I'd suggest that if you don't know how to do IO yet then you probably want to learn how that works thoroughly before you start getting into more advanced topics like reflection.

YATArchivist at 2007-7-7 11:23:17 > top of Java-index,Security,Event Handling...
# 2

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 ?

eiffel65 at 2007-7-7 11:23:17 > top of Java-index,Security,Event Handling...
# 3
Yes, that's possible; have a look at [url= http://www.beanshell.org]BeanShell[/url]kind regards,Jos
JosAH at 2007-7-7 11:23:17 > top of Java-index,Security,Event Handling...
# 4

> 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

lutha_brother at 2007-7-7 11:23:17 > top of Java-index,Security,Event Handling...
# 5

> 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?

UlrikaJ at 2007-7-7 11:23:17 > top of Java-index,Security,Event Handling...
# 6
Are you the real EIFFEL65 BAND
kia_vokova@yahoo.com.my at 2007-7-7 11:23:17 > top of Java-index,Security,Event Handling...