Solved

writing to a file every 1 Minute!!

Posted on 2004-09-15
7
208 Views
Last Modified: 2010-03-31
how can i solve the problem in java: java-program that write to file every 1 minute?? can u please help?? please submit a sample code if u can??


 
 

0
Comment
Question by:aimueller
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12063373
Use java.util.Timer
0
 
LVL 21

Expert Comment

by:MogalManic
ID: 12063644
More specificaly:

MyTimerTask task=new MyTimerTask();
Timer t=new Timer();
t.schedule(task, startTime, 1000*60) ;
...

Class MyTimerTask extends TimerTask
{
  public void run()
  {
...Scheduled activity...
  }
}


The java.util.Timer class is new to JDK 1.4.  If you are using an older JDK you will have to write something using Threads.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12063662
>>The java.util.Timer class is new to JDK 1.4.  

1.3 actually
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 37

Expert Comment

by:zzynx
ID: 12064016
>> please submit a sample code if u can

This appends a line of text in the file log.txt at directory C:\dir1\dir2 every minute:

/*
 * TimerDemo.java
 *
 */

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

/**
 *
 * @author  zzynx
 */
public class TimerDemo {
   
    public TimerDemo() {
        new Timer().schedule(new TimerTask() {
            public void run() {
                File f = new File("c:/dir1/dir2/log.txt");
                try {
                    FileWriter writer = new FileWriter(f, true);
                    writer.write(new Date().toString() + " Another line written");
                    writer.write(10);
                    writer.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }, new Date(), 1000*60);
    }
   
    public static void main (String args[]) {
        new TimerDemo();
    }
}

The file looks like:

Wed Sep 15 14:57:34 CEST 2004 Another line written
Wed Sep 15 14:58:34 CEST 2004 Another line written
Wed Sep 15 14:59:34 CEST 2004 Another line written
Wed Sep 15 15:00:34 CEST 2004 Another line written
Wed Sep 15 15:01:34 CEST 2004 Another line written
Wed Sep 15 15:02:34 CEST 2004 Another line written
0
 

Author Comment

by:aimueller
ID: 12072515
thank u zzynx. In this case the file "log.txt" is opened and closed every minute. can u change the code, that the run() method work on a filestream (writer) may be as global variable??

thank u

andy
0
 
LVL 37

Accepted Solution

by:
zzynx earned 50 total points
ID: 12072561
Like this?

/*
 * TimerDemo.java
 *
 */

import java.util.*;
import java.io.*;
/**
 *
 * @author  zzynx
 */
public class TimerDemo {
   
    private FileWriter writer = null;
   
    public TimerDemo() {
        File f = new File("c:/gd/swap/log.txt");
        try {
            writer = new FileWriter(f, true);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
       
        new Timer().schedule(new TimerTask() {
            public void run() {
                try {
                    writer.write(new Date().toString() + " Another line written");
                    writer.write(10);
                    writer.flush();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }, new Date(), 1000*15);
    }
   
    public static void main (String args[]) {
        new TimerDemo();
    }
}
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12074135
Thanks for accepting.

PS. For my future comments: can you explain me what you've wanted more to award me with an A-grade?
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question