home *** CD-ROM | disk | FTP | other *** search
/ Java for 3D & VRML Worlds / Java for 3d and VRML Worlds.iso / examples / chap05 / Motd.java < prev    next >
Encoding:
Java Source  |  1996-11-25  |  1.2 KB  |  45 lines

  1. // Motd.java
  2. // This program checks if file exists or not.
  3. import vrml.*;
  4. import vrml.node.*;
  5. import vrml.field.*;
  6. import java.io.*;
  7.  
  8. public class Motd extends Script {
  9.    String myFile = "C:/tmp/motd"; // message of today
  10.    Browser b;
  11.    DataInputStream in;
  12.    String message;  
  13.  
  14.    public void initialize() {
  15.       b = getBrowser(); // to display error message
  16.    }
  17.   
  18.    public void processEvent(Event ev){
  19.       File f = null;
  20.       // open file
  21.       if(ev.getName().equals("clicked")){
  22.          try {
  23.             f = new File(myFile);
  24.             in = new DataInputStream(new FileInputStream(f));
  25.          } catch (Exception e){
  26.             b.setDescription("can not open file: " + myFile);  
  27.             e.printStackTrace();
  28.          }
  29.  
  30.          // if it exists, read it
  31.          try {
  32.             if(f.exists()){
  33.                message = in.readLine();
  34.                // display the message of today.
  35.                b.setDescription(message); 
  36.             } else {
  37.                b.setDescription("file does not exists: " + myFile);  
  38.             }
  39.          } catch (Exception e){
  40.             e.printStackTrace();
  41.          }
  42.       }
  43.    }
  44. }
  45.