Solved

writing to a file every 1 Minute!!

Posted on 2004-09-15
7
204 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

929 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

12 Experts available now in Live!

Get 1:1 Help Now