Solved

writing to a file every 1 Minute!!

Posted on 2004-09-15
7
206 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
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.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
windows explorer path to command prompt 5 54
hibernate insert example 13 34
JAVA API design with micro service cloud in mind 1 47
jmss example java 2 23
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

828 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