Solved

writing to a file every 1 Minute!!

Posted on 2004-09-15
7
201 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
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Use java.util.Timer
0
 
LVL 21

Expert Comment

by:MogalManic
Comment Utility
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
Comment Utility
>>The java.util.Timer class is new to JDK 1.4.  

1.3 actually
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now