home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 27 / CDROM27.iso / share / wnt / jig / data1.cab / Program_Executable_Files / src / FileViewer / FileViewer.java < prev   
Encoding:
Java Source  |  1998-08-19  |  2.0 KB  |  57 lines

  1. // This example is from the book _Java in a Nutshell_ by David Flanagan.
  2. // Written by David Flanagan.  Copyright (c) 1996 O'Reilly & Associates.
  3. // You may study, use, modify, and distribute this example for any purpose.
  4. // This example is provided WITHOUT WARRANTY either expressed or implied.
  5.  
  6. import java.awt.*;
  7. import java.io.*;
  8.  
  9. public class FileViewer extends Frame {
  10.     Button close;
  11.     // Query the size of the specified file, create an array of bytes big
  12.     // enough, and read it in.  Then create a TextArea to display the text
  13.     // and a "Close" button to pop the window down.
  14.     public FileViewer(String filename) throws IOException {
  15.         super("FileViewer: " + filename);
  16.         File f = new File(filename);
  17.         int size = (int) f.length();
  18.         int bytes_read = 0;
  19.         FileInputStream in = new FileInputStream(f);
  20.         byte[] data = new byte[size];
  21.         while(bytes_read < size)
  22.             bytes_read += in.read(data, bytes_read, size-bytes_read);
  23.         
  24.         TextArea textarea = new TextArea(new String(data, 0), 24, 80);
  25.         textarea.setFont(new Font("Helvetica", Font.PLAIN, 12));
  26.         textarea.setEditable(false);
  27.         this.add("Center", textarea);
  28.         
  29.         close = new Button("Close");
  30.         this.add("South", close);
  31.         this.pack();
  32.         this.show();
  33.     }
  34.     
  35.     // Handle the close button by popping this window down 
  36.     public boolean action(Event e, Object what) {
  37.         if (e.target == close) {
  38.             this.hide();
  39.             this.dispose();
  40.             return true;
  41.         }
  42.         return false;
  43.     }
  44.     
  45.     // The FileViewer can be used by other classes, or it can be
  46.     // used standalone with this main() method.
  47.     static public void main(String[] args) throws IOException {
  48.         if (args.length != 1) {
  49.             System.out.println("Usage: java FileViewer <filename>");
  50.             System.exit(0);
  51.         }
  52.         try { Frame f = new FileViewer(args[0]); }
  53.         catch (IOException e) { System.out.println(e); }
  54.     }
  55. }
  56.  
  57.