|
All Methods zio package context |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object zio.Input
Provides for text input from files
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();
Error handling can be overridden by subclassing the Consider using 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.
Zio
class.
To change token separators, create a subclass and override the
notATokenChar()
method. To change maximum token size, use the
appropriate constructor.
File
class in conjunction with this
class; that way offers greater flexibility over the direct use of file names.
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 |
public Input()
public Input(java.lang.String fileName)
fileName
- the name of the file to be read, can be a path namepublic Input(java.io.File file)
file
- a File object
public Input(java.io.File file, int maxTokenSize)
file
- a File objectmaxTokenSize
- the maximum number of characters in a tokenpublic Input(java.lang.String fileName, int maxTokenSize)
fileName
- the name of the file to be read, can be a path namemaxTokenSize
- the maximum number of characters in a tokenpublic Input(int maxTokenSize)
maxTokenSize
- the maximum number of characters in a tokenpublic Input(java.io.Reader reader)
reader
- any reader objectpublic Input(java.io.Reader reader, int maxTokenSize)
reader
- any reader objectpublic Input(java.io.InputStream stream)
stream
- any InputStream object
public Input(java.io.InputStream stream, int maxTokenSize)
stream
- any InputStream objectmaxTokenSize
- the maximum number of characters in a tokenMethod Detail |
protected boolean notATokenChar(int c)
public final char readChar()
readChar()
.
public final java.lang.String readLine()
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.
public final java.lang.String findAndReadToken()
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.
What is meant by a token can be changed by
overriding the notATokenChar
method.
public final int findAndReadInt()
Integer.MIN_VALUE
if no token is found.
Causes an error if token cannot be translated
to int
form.
public final double findAndReadDouble()
Double.NaN
if no token is found.
Causes an error if token cannot be translated to
double
form.
public final boolean eof()
public final void close()
Input
or Output
object.
|
author context |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |