home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / javafile / ch02 / More.java < prev    next >
Encoding:
Java Source  |  1998-12-14  |  1.7 KB  |  67 lines

  1. import java.io.*;
  2.  
  3. /*
  4.  * More.class similar to UNIX more utility
  5.  */
  6. class More {
  7.  
  8. public static void main (String args[])
  9. {
  10.        String buf;
  11.        FileInputStream fs=null;
  12.        int nlines;
  13.  
  14.        if (args.length != 1) {
  15.                System.out.println ("usage: java More <file>");
  16.                System.exit (1);
  17.         }
  18.  
  19. /*
  20.  * Try to open the filename specified by args[0]
  21.  */
  22.        try {
  23.                fs = new FileInputStream (args[0]);
  24.        } catch (Exception e) {
  25.                System.out.println (e);
  26.                System.exit (1);
  27.         }
  28.         
  29. /*
  30.  * Create a BufferedReader associated with FileInputStream fs
  31. *
  32. */
  33.       BufferedReader ds = new BufferedReader(new InputStreamReader(fs));
  34. BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
  35.  
  36.  
  37.         nlines = 0;
  38.         while (true) {
  39.                try {
  40.                       buf = ds.readLine ();        // read 1 line
  41.                       if (buf == null) break;
  42.                } catch (IOException e) {
  43.                       System.out.println (e);      
  44.                       break;
  45.                }
  46.                System.out.println (buf); 
  47. nlines += 1;
  48.                if (nlines % 23 == 0) {  // 23 lines/pages vt100
  49. System.out.print ("-More-");
  50.                       System.out.flush ();
  51.                       try {
  52.                               keyboard.readLine ();
  53.                       } catch (IOException e) {
  54.                       }
  55.                }
  56.         }
  57. /*
  58.  * close can throw an exception also, 
  59.  * and catch it for completeness
  60. */
  61.          try {
  62.                fs.close ();
  63.        } catch (IOException e) {
  64.                System.out.println (e);
  65.         }
  66. }
  67. }