Solved

writing to a file every 1 Minute!!

Posted on 2004-09-15
7
207 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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
iterator/ListIterator approach 17 68
jsp login check 12 67
difference between sorce folder and folder in eclipise 3 65
ejb mdb examples 1 39
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

710 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