Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 

Read a file of reminders, sleep until each is due, beep. Java code example - Click here to copy ->>>

   Can't find what you're looking for? Try our search:

Really working examples categorized by API, package, class. You can compile and run our examples right away! Not from source code for Java projects - only working examples! Copy, compile and run!
------------------

Code:

 
/*
 * Copyright (c) Ian F. Darwin, http://www.darwinsys.com/, 1996-2002.
 * All rights reserved. Software written by Ian F. Darwin and others.
 * $Id: LICENSE,v 1.8 2004/02/09 03:33:38 ian Exp $
*/

/*
 * Copyright (c) Ian F. Darwin, http://www.darwinsys.com/, 1996-2002.
 * All rights reserved. Software written by Ian F. Darwin and others.
 * $Id: LICENSE,v 1.8 2004/02/09 03:33:38 ian Exp $
*/


import java.io.*;
import java.text.*;
import java.util.*;
import javax.swing.*;

/**
 * Read a file of reminders, sleep until each is due, beep.
 * Much of this logic has been uperceded by java.util.Timer, which is used
 * in the non-Old version of this program.
 * @author Ian F. Darwin, http://www.darwinsys.com/
 * @version $Id: ReminderServiceOld.java,v 1.2 2004/02/09 03:33:46 ian Exp $
 */
public class ReminderServiceOld {
   class Item {
      Date due;
      String message;
      Item(Date d, String m) {
         due = d;
         message = m;
      }
   }

   ArrayList l = new ArrayList();

   public static void main(String[] argv) throws IOException {
      ReminderServiceOld rs = new ReminderServiceOld();
      rs.load();
      rs.run();
   }

   protected void load() throws IOException {

      BufferedReader is = new BufferedReader(
         new FileReader("ReminderService.txt"));
      SimpleDateFormat formatter =
         new SimpleDateFormat ("yyyy MM dd hh mm");
      String aLine;
      while ((aLine = is.readLine()) != null) {
         ParsePosition pp = new ParsePosition(0);
         Date date = formatter.parse(aLine, pp);
         if (date == null) {
            message("Invalid date in " + aLine);
            continue;
         }
         String mesg = aLine.substring(pp.getIndex());
         l.add(new Item(date, mesg));
      }
   }

   public void run() {
      System.out.println("ReminderServiceOld: Starting at " + new Date());
      while (!l.isEmpty()) {
         Date d = new Date();
         Item i = (Item)l.get(0);
         long interval = i.due.getTime() - d.getTime();
         if (interval > 0) {
            System.out.println("Sleeping until " + i.due);
            try {
               Thread.sleep(interval);
            } catch (InterruptedException e) {
               System.exit(1);   // unexpected intr
            }
            message(i.due + ": " + i.message);
         } else
            message("MISSED " + i.message + " at " + i.due);
         l.remove(0);
      }
      System.exit(0);
   }
   void message(String message) {
      System.out.println("\007" + message);
      JOptionPane.showMessageDialog(null,
         message,
         "Timer Alert",            // titlebar
         JOptionPane.INFORMATION_MESSAGE);   // icon
   }
}

 
 



References.

The list of classes which were used on this page you can find below. The links to Java API contain official SUN documentation about all used classes.

  java.util.ArrayList

  java.sql.Date

  java.io.IOException

  java.io.BufferedReader

  java.io.FileReader

  java.text.SimpleDateFormat

  java.text.ParsePosition

  org.omg.CORBA.DynAnyPackage.Invalid

  java.lang.InterruptedException

  java.util.Timer




[ Go Back ]



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy