package klasse5; /* THE JAVA TEXT PROGRAM 28 April 1999 Copyright (c) 1999 Per Brinch Hansen This program defines a set of Java classes for text processing. These text classes enable Java programs to output text to screen and disk files and input text from keyboard and disk files. The present version runs on Macintosh, Unix, and Windows 95 systems. Please follow these instructions to install the text classes on a Unix system: 1. Create a directory for your Java programs; 2. Move this program text to your program directory as a file, named text.java; 3. If you received the program text by email, use an editor to delete everything above the first line and everything below the last line of the program; 4. Compile the text program into executable Java code. The compilation adds 11 code files to your program directory: basic.class infile.class input.class keyboard.class outfile.class output.class random.class reader.class screen.class test.class writer.class 5. Run a simple test of the text program by executing the compiled class, named test, as a self-contained Java program. If the compilation was successful, you will be asked to type your name, followed by return. When you have typed your name, say, Jane Doe, the program responds with the message: Welcome to Java, Jane Doe! The basic class defines Unix as the default system: public static final int OS = Unix; If you wish to compile the text classes on a Macintosh (or Windows 95) system, you must first use an editor to replace the word, Unix, by the word, Mac (or Win95). Please leave the text file, text.java, and the compiled classes in your program directory. Java programs that use the text classes must be compiled and run in the same directory. The following example shows a user program, named typist, that uses the text classes: class typist extends basic { public static void main(String param[]) throws Exception { input in = new input(); output out = new output("results"); while (in.more()) out.write(in.read()); in.close(); out.close(); } } The program extends the basic class. The program copies text from the keyboard to a new disk file, named results. The input ends when you type a line consisting of the word, eof, followed by return. ------------------------------------------------------------ Modifications by H.Moessenboeck, August 1999 - Class basic exports static variables in and out that represent the standard input and output stream. Methods of class input and output don't throw exceptions any more. Thus a hello world program becomes as simple as class Hello extends basic { public static void main (String[] arg) { out.writeln("hello world"); } } - All methods of class input set a boolean variable done, which indicates if the operation was successful. For example, reading a sequence of numbers becomes: x = in.readint(); while (in.done) { ... x = in.readint(); } - The constructor as well as the close method of class output set a boolean variable done indicating the success of the operation. - Input and output files are always allocated and searched in the directory that contains the class files. - Deprecated method DataInputStream.readLine avoided. - Class random has a new method to set the seed, which is now an instance variable instead of a static variable. ------------------------------------------------------------*/ import java.io.*; import java.lang.Math; /* THE BASIC CLASS ============================================================= Defines common constants and methods. */ class basic { public static final int Mac = 0, Unix = 1, Win95 = 2, OS = Win95; public static final String EOF = "eof"; public static final char cr = '\r', eof = '\uFFFF', nl = '\n', sp = ' '; public static output out; public static input in; static /*initializer*/ { try { out = new output(); in = new input(); } catch (Exception e) { error("could not open standard input/output"); } } public static void assume(boolean condition) { assume(condition, "invalid assumption"); } public static void assume(boolean condition, String msg) { if (!condition) error(msg); } public static void error(String msg) { out.writeln("*** error: " + msg); } } /* THE WRITER INTERFACE ========================================================= Defines a writer as any class that includes a write method for outputting a single character and a close method for terminating output. */ interface writer { public void close() throws Exception; public void write(char value); } /* THE SCREEN CLASS ========================================================== Implements a writer interface for outputting single characters only to a screen. */ class screen extends basic implements writer { public screen() { } public void close() { } public void write(char value) { System.out.print(value); } } /* THE OUTFILE CLASS ========================================================== Implements a writer interface for outputting single characters only to a disk file. (Uses the class, PrintStream, that has been deprecated, but is still available in JDK 1.1.5.) */ class outfile extends basic implements writer { private PrintStream out; public outfile(String name) throws Exception { out = new PrintStream(new FileOutputStream(name)); } public void close() throws Exception { if (out != null) { out.close(); out = null; } } public void write(char value) { if (value == nl) switch (OS) { case Mac: out.print(cr); break; case Unix: out.print(nl); break; case Win95: out.print(cr); out.print(nl); break; } else out.print(value); } } /* THE OUTPUT CLASS ========================================================== Used to output a text file to a screen or disk. */ class output extends basic { private writer out; public boolean done; public output() { out = new screen(); done = true; } public output(String name) { try { out = new outfile(name); done = true; } catch (Exception e) { error("could not open output file " + name); done = false; } } public void close() { try { if (out != null) { out.close(); out = null; } done = true; } catch (Exception e) { error("could not close output file"); done = false; } } public void write(boolean value) { write(value, 1); } public void write(boolean value, int width) { String word = (String)(value?"true":"false"); writespace(width - word.length()); write(word); } public void write(char value) { out.write(value); } public void write(char value, int width) { write(value); writespace(width - 1); } public void write(double value) { write(value, 1); }