All 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 command windows, sockets, character arrays, Strings and File objects.

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();

Input operates in three modes: char, token, and line. In char mode, end of line chars are read. In token mode, white space including end of line chars are skipped. The first example above is an example of token mode. (There is a maximum token size which can be overridden with a constructor.) In line mode, end of line chars are thrown away. The second example above is an example of line mode. You can switch between modes at will but when switching to line mode, the rest of the current input line will be thrown away. The eof() method works the same way in all modes: you try to read something first and then ask whether an end of file was encountered.

Error handling can be overridden by subclassing the Zio class. To change token separators, create a subclass and override the notATokenChar() method. To change maximum token size, use the appropriate constructor.

Consider using the File class in conjunction with this class; that way offers greater flexibility over the direct use of file names.

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.io.File file)
          Creates an input object from a File object.
Input(java.io.File file, int maxTokenSize)
          Creates an input object from a File Object.
Input(java.io.InputStream stream)
          Creates an Input object from any InputStream object.
Input(java.io.InputStream stream, int maxTokenSize)
          Creates an Input object from any InputStream object.
Input(int maxTokenSize)
          Creates an input object capable of reading from the keyboard.
Input(java.io.Reader reader)
          Creates an input object capable of using any Reader object.
Input(java.io.Reader reader, int maxTokenSize)
          Creates an input object capable of using any Reader object.
Input(java.lang.String fileName)
          Creates an input object capable of reading from the named file.
Input(java.lang.String fileName, int maxTokenSize)
          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.
protected  boolean notATokenChar(int c)
          Create a subclass and override this method to obtain different token separation.
 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

Input


public Input(java.io.File file)
Creates an input object from a File object.

Parameters:
file - a File object
maximum token size is 36

Input


public Input(java.io.File file,
             int maxTokenSize)
Creates an input object from a File Object.

Parameters:
file - a File object
maxTokenSize - the maximum number of characters in a token

Input


public Input(java.lang.String fileName,
             int maxTokenSize)
Creates an input object capable of reading from the named file.

Parameters:
fileName - the name of the file to be read, can be a path name
maxTokenSize - the maximum number of characters in a token

Input


public Input(int maxTokenSize)
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

Parameters:
maxTokenSize - the maximum number of characters in a token

Input


public Input(java.io.Reader reader)
Creates an input object capable of using any Reader object. Use this constructor with a CharArrayReader or a StringReader object to "read" from a character array or a string using the same Input class methods you use to read from a file or a keyboard.
maximum token size is 36

Parameters:
reader - any reader object

Input


public Input(java.io.Reader reader,
             int maxTokenSize)
Creates an input object capable of using any Reader object. Use this constructor with a CharArrayReader or a StringReader object to "read" from a character array or a string using the same Input class methods you use to read from a file or a keyboard.
maximum token size is 36

Parameters:
reader - any reader object

Input


public Input(java.io.InputStream stream)
Creates an Input object from any InputStream object. Use this constructor with sock.getInputStream() to perform input from a Socket object.

Parameters:
stream - any InputStream object
maximum token size is 36

Input


public Input(java.io.InputStream stream,
             int maxTokenSize)
Creates an Input object from any InputStream object. Use this constructor with sock.getInputStream() to perform input from a Socket object.

Parameters:
stream - any InputStream object
maxTokenSize - the maximum number of characters in a token
Method Detail

notATokenChar


protected boolean notATokenChar(int c)
Create a subclass and override this method to obtain different token separation. Your method will be passed an int containing the next character in the file. It should return a boolean indicating whether that char is acceptable within a token. Chars not acceptable within tokens will be treated the way white space characters are currently treated.


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().

If you switch to char-by-char reading from line-by-line reading the first char you will get is the first char of the line after the line you just read. If you switch to char-by-char reading from token-by-token reading, the first char you will get is the first white space char after the token you just read. That may be an end of line character.

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.

If your program switches to line mode after having read something in character or token mode, the rest of the current line will be skipped and the first line you get will be the one after that. If your program only reads lines, this will not be a problem.

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.

There is a maximum token size of 36 which may affect how many characters are actually read. You can override this maximum token size by passing an extra parameter to a constructor.

What is meant by a token can be changed by overriding the notATokenChar 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. When using another character-by-character Reader object, eof() becomes true when there are no more characters to "read".

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