Basic Methods
zio package

context

zio
Class Input


java.lang.Object

  extended byzio.Input


public class Input
extends java.lang.Object

Provides for text input from files and command windows .

Simple Examples

 // This example reads 10 ints from the keyboard.  Ints may be
 // entered on one or more lines.  Ints on the same line must be
 // separated by spaces.
     Input ink = new Input();  // input from keyboard
     int [] ary = new int [10];
     for(int i=1; i<=10; i+=1) {
        ary[i] = ink.findAndReadInt();
     }
 // next example copies a file, line by line from "infile.txt" to 
 // "outfile.txt" using the Output class with the Input class
     Input in = new Input("infile.txt");
     Output out = new Output("outfile.txt");
     for(;;) {
         String line = in.readLine();
         if( in.eof() ) break;
         out.writeString(line); out.writeEndOfLine();
     }
     in.close(); out.close();

Version:
Jul 22, 2005
Author:
copyright 2005 by J Adrian Zimmer
Licensed under the Open Software License version 2.1

Constructor Summary
Input()
          Creates an input object capable of reading from the keyboard.
Input(java.lang.String fileName)
          Creates an input object capable of reading from the named file.
 
Method Summary
 void close()
          Tells operating system your program is finished reading.
 boolean eof()
          Returns true if the last attempt to read failed because of the end of the input was discovered during that particular read operation.
 double findAndReadDouble()
          Reads the next real number.
 int findAndReadInt()
          Reads the next integer.
 java.lang.String findAndReadToken()
          Reads the next token.
 char readChar()
          Reads the next character of input.
 java.lang.String readLine()
          Reads the next line of input.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Input


public Input()
Creates an input object capable of reading from the keyboard. The command window must have the focus. (That pretty much means the user must have clicked on the window used to run the program which uses this constructor.)
maximum token size is 36


Input


public Input(java.lang.String fileName)
Creates an input object capable of reading from the named file.
maximum token size is 36

Parameters:
fileName - the name of the file to be read, can be a path name
Method Detail

readChar


public final char readChar()
Reads the next character of input. Any character, printable or not, can be read this way. This means end of line characters, too. The maximum token size has no effect on readChar().

Returns:
the next char of input

readLine


public final java.lang.String readLine()
Reads the next line of input. Use this method to read your input line by line. Each line is returned in its entirety except for the (usually invisible) characters used by the operating system mark the ends of lines. Lines can be any length, the maximum token size has no effect here.

Returns:
the next line of input without its trailing end of line char(s)

findAndReadToken


public final java.lang.String findAndReadToken()
Reads the next token. This method first skips white space and then reads a token. This means that several blank lines could be skipped over and thrown away before the next token is found and read.

A token is rather like a word, it is a sequence of nonblank chars that is surrounded by blanks, tabs, or line endings. For example, "hi", "23", and "R2D2". Unlike words, punctuation and numbers are considered part of tokens. So if there really are quotation marks in the input, they will be found in the string returned by this method.

Returns:
the next token

findAndReadInt


public final int findAndReadInt()
Reads the next integer. This method actually reads a token first and then translates that token to int form. Returns Integer.MIN_VALUE if no token is found. Causes an error if token cannot be translated to int form.

Returns:
the next integer

findAndReadDouble


public final double findAndReadDouble()
Reads the next real number. This method actually reads a token first and then translates that token to double form. Returns Double.NaN if no token is found. Causes an error if token cannot be translated to double form.

Returns:
the next double

eof


public final boolean eof()
Returns true if the last attempt to read failed because of the end of the input was discovered during that particular read operation. Usually you will use this to find the end of a text file. When reading from the keyboard, eof()'s behavior is operating system dependent.

Returns:
true iff the last attempt to read failed because the end of input was encountered

close


public final void close()
Tells operating system your program is finished reading. Always close files when you are done with them. Files can be reopened with by instantiating another Input or Output object.


author
context