For Programmers: Free Programming Magazines  


Home > Archive > Java Help > July 2004 > Continuously Execute a program









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Continuously Execute a program
Stephen Curtin

2004-07-22, 3:57 am

Hi

I need help with a java program. I have written a program that reads
file1, which is outputted by another program. Within file1 is the
location of file2. It gets file2 from the location sepcified and
extracts the relevant information from it and creates file3. the
program then reads the contents of file3 and outputs it to a JTable in
a JPanel (along with some other calculations)

The problem is that file1 changes every hour or so and the contents of
file2 change all the time. I need some way of continuously executing
"main" so that it will pick up these changes and then I can repaint
the JTable.

Thanks
Steve
Alex Hunsley

2004-07-22, 8:57 am

Stephen Curtin wrote:

> Hi
>
> I need help with a java program. I have written a program that reads
> file1, which is outputted by another program. Within file1 is the
> location of file2. It gets file2 from the location sepcified and
> extracts the relevant information from it and creates file3. the
> program then reads the contents of file3 and outputs it to a JTable in
> a JPanel (along with some other calculations)
>
> The problem is that file1 changes every hour or so and the contents of
> file2 change all the time. I need some way of continuously executing
> "main" so that it will pick up these changes and then I can repaint
> the JTable.
>
> Thanks
> Steve


Normally whem you create a GUI, the 'main' thread drops through and returns,
but your program is still running as there is a GUI that has been realised (is
on-screen) and is waiting for events to happen.
One way of doing your task is to somewhere along the way create a thread that
periodically checks the files you're interested in for changes - if there are
any changes, it can update the GUI. When it's not checking, it can be doing
Thread.sleep(...) to wait for the next check.
I'm not sure if java supports file monitoring such that you could get a
callback on file change, but I've had a quick look and can't see anything like
it...

alex


Real Gagnon

2004-07-22, 8:57 am

> The problem is that file1 changes every hour or so and the contents of
> file2 change all the time. I need some way of continuously executing
> "main" so that it will pick up these changes and then I can repaint
> the JTable.


Check http://www.rgagnon.com/javadetails/java-0214.html
for an example using the Observer/Observable mechanism.

Bye.
--
Real Gagnon from Quebec, Canada
* Looking for Java or PB snippets ? Visit Real's How-to
* http://www.rgagnon.com/howto.html
Stephen Curtin

2004-07-22, 3:59 pm

I have included a sample of code here that should simplify my problem.
This code reads a line from a file and prints it to the screen. What
I want to do is add a new line to the text file at runtime and have
the program pick it up and write it to the screen. Is this possible?

Thanks!

import java.io.*;
public class DynamicReader {
public static void main(String[] args) {
String fileName = "testFile.txt";
String currentLine = "";
try {
BufferedReader buffer =
new BufferedReader(new FileReader(fileName));

while (buffer.ready()) {
currentLine = buffer.readLine();
for (int i = 0; i < currentLine.length(); i++) {
System.out.print(currentLine.charAt(i));
}
System.out.println();
}
} catch (IOException e) {
System.out.println("File read error");
}
}
}
Alex Hunsley

2004-07-23, 3:58 pm

Stephen Curtin wrote:
> I have included a sample of code here that should simplify my problem.
> This code reads a line from a file and prints it to the screen. What
> I want to do is add a new line to the text file at runtime and have
> the program pick it up and write it to the screen. Is this possible?
>
> Thanks!
>


Yes. You need to learn how start another thread in java. Then have this
threads' run method have a loop that sleeps mostly, and sometimes checks the
file for changes. If it finds a change, it notifies listeners (see
publish/subscribe patterns).

alex

Alex Hunsley

2004-07-23, 3:58 pm

Real Gagnon wrote:

>
>
> Check http://www.rgagnon.com/javadetails/java-0214.html
> for an example using the Observer/Observable mechanism.
>
> Bye.


ah, an example! that will help the guy.
Two reservations I have the about the code though:

it uses a swing timer when it shouldn't (swing timers are for scheduling gui
events on the GUI thread such as repaints, *not* for non-gui things like disc
access, especially since disk access may be time consuming and block the gui
thread.)

Also, it uses the generic observer interface - this make less readable code -
much better to define your own listener interface - slightly more verbose but
then someone reading the code knows exactly what type of thing it is listening
to, especially when the handling listener method is called something like
'handleFileChanged'.

alex

Alex Hunsley

2004-07-23, 3:58 pm

Alex Hunsley wrote:

> Real Gagnon wrote:
>
>
>
> ah, an example! that will help the guy.
> Two reservations I have the about the code though:
>
> it uses a swing timer when it shouldn't (swing timers are for scheduling
> gui events on the GUI thread such as repaints, *not* for non-gui things
> like disc access, especially since disk access may be time consuming and
> block the gui thread.)
>
> Also, it uses the generic observer interface - this make less readable
> code - much better to define your own listener interface - slightly more
> verbose but then someone reading the code knows exactly what type of
> thing it is listening to, especially when the handling listener method
> is called something like 'handleFileChanged'.


- forgot to say, also things get a bit messy when your object is using the
generic observer stuff to observe several things!

alex


Roedy Green

2004-07-23, 3:58 pm

On Fri, 23 Jul 2004 15:33:36 +0100, Alex Hunsley
<lard@tardis.ed.ac.molar.uk> wrote or quoted :

>it uses a swing timer when it shouldn't (swing timers are for scheduling gui
>events on the GUI thread such as repaints, *not* for non-gui things like disc
>access, especially since disk access may be time consuming and block the gui
>thread.)


for hints on timers, see
http://mindprod.com/jgloss/timer.html
http://mindprod.com/jgloss/time.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com