|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||
java.lang.Object
|
+--java.io.Reader
|
+--java.io.FilterReader
|
+--ucb.io.FormatReader
Provides formatted input of text, with format specifications similar to scanf from the standard C library. Each FormatReader is connected to an InputStream or Reader from which it extracts characters. Under control of a format argument, the member function scan will read characters from the the connected stream and convert them into a sequence of InputItems. Here is a typical use of this class for formatted input (assume stream is a FormatReader):
// Read a string of up to 5 characters, followed by two integers,
// separated by whitespace.
stream.scan("%5s%d%d");
// Exit if end-of-file occurred before first data item could
// be read.
if (stream.eof())
break;
// Test for error.
if (stream.error())
ReportError("bad input");
// stream now contains 3 items scanned from the input stream to which
// it is connected.
name = stream.item(0).toString(); // The first token read (a string)
size = stream.item(1).toInt(); // The second token read (an int)
number = stream.intItem(2); // The third token read (shorthand
// for stream.item(2).toInt()).
InputItem| Fields inherited from class java.io.FilterReader |
in |
| Fields inherited from class java.io.Reader |
lock |
| Constructor Summary | |
FormatReader(java.io.InputStream inp)
A new FormatReader filtering input from INP. |
|
FormatReader(java.io.Reader inp)
A new FormatReader filtering input from INP. |
|
| Method Summary | |
int |
charCount()
Number of characters consumed by the last call to scan. |
char |
charItem(int i)
Short for item(i).toChar(). |
void |
clear(boolean flag)
Set the return value of error() to the indicated value. |
void |
close()
Closes this stream and its source Reader and releases associated system resources. |
double |
doubleItem(int i)
Short for item(i).toDouble(). |
boolean |
eof()
True if the last call to scan encountered end-of-file. |
boolean |
error()
Set true when a call to scan encounters an error in the input. |
float |
floatItem(int i)
Short for item(i).toFloat(). |
int |
getc()
Returns the next character, as would be read by scan("%c"), and returns either it, or -1 on end of file or error. |
int |
intItem(int i)
Short for item(i).toInt(). |
InputItem |
item(int i)
A value scanned by the last call to scan. |
int |
itemCount()
Number of InputItems set by last call to scan. |
long |
longItem(int i)
Short for item(i).toLong(). |
void |
mark(int readAheadLimit)
|
boolean |
markSupported()
|
int |
read()
|
int |
read(char[] cbuf)
|
int |
read(char[] cbuf,
int off,
int len)
|
boolean |
ready()
|
void |
reset()
|
void |
scan(java.lang.String format)
Extract data items from the attached InputStream according to a specified format string, and put them in item(0), item(1), etc. |
void |
skip(int n)
|
java.lang.String |
stringItem(int i)
Short for item(i).toString(). |
void |
tie(java.io.OutputStream out)
Arrange for OUT to be flushed before every input operation on THIS. |
void |
tie(java.io.Writer out)
Arrange for OUT to be flushed before every input operation on THIS. |
void |
unread(int ch)
Insert character at the beginning of the remaining input stream. |
| Methods inherited from class java.io.FilterReader |
skip |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
public FormatReader(java.io.Reader inp)
public FormatReader(java.io.InputStream inp)
| Method Detail |
public void scan(java.lang.String format)
throws FormatException
For each conversion specification other than `%' and those that are flagged with an asterisk, scan places the value represented by the next field in the input stream into an InputItem, which is retrievable by the item member function. An input field is defined as a string of characters that extends to the next character that is inappropriate for the given conversion specification (so that, for example, a comma or space will delimit a field that is supposed to be an integer), or until the the field width, when specified, is filled, whichever is shorter. With the exception of the `%c' and `%[' conversion specifications, the conversion first skips leading white space, and white space characters are always deemed "inappropriate".
The valid conversion codes are as follows:
format - the specification for the conversions to be performed.FormatException - raised if the format string has an
invalid form.InputItem,
item(int)public void unread(int ch)
ch - the character to insert.public void tie(java.io.OutputStream out)
out - the OutputStream that is to be flushed.OutputStream.flush()public void tie(java.io.Writer out)
out - the Writer that is to be flushed.Writer.flush()public boolean eof()
error(),
scan(java.lang.String)public boolean error()
clear(boolean)public void clear(boolean flag)
flag - value that next call to error() will return.error()public int itemCount()
scan(String),
item(int)public int charCount()
scan(String)
public InputItem item(int i)
throws java.lang.IndexOutOfBoundsException
java.lang.IndexOutOfBoundsException - if i is negative or greater
than itemCount()-1.
public int intItem(int i)
throws java.lang.IndexOutOfBoundsException,
java.lang.IllegalArgumentException
public long longItem(int i)
throws java.lang.IndexOutOfBoundsException,
java.lang.IllegalArgumentException
public char charItem(int i)
throws java.lang.IndexOutOfBoundsException,
java.lang.IllegalArgumentException
public int getc()
public float floatItem(int i)
throws java.lang.IndexOutOfBoundsException,
java.lang.IllegalArgumentException
public double doubleItem(int i)
throws java.lang.IndexOutOfBoundsException,
java.lang.IllegalArgumentException
public java.lang.String stringItem(int i)
throws java.lang.IndexOutOfBoundsException,
java.lang.IllegalArgumentException
public void close()
close in class java.io.FilterReader
public int read()
throws java.io.IOException
read in class java.io.FilterReader
public int read(char[] cbuf,
int off,
int len)
throws java.io.IOException
read in class java.io.FilterReader
public int read(char[] cbuf)
throws java.io.IOException
read in class java.io.Reader
public void reset()
throws java.io.IOException
reset in class java.io.FilterReaderpublic boolean markSupported()
markSupported in class java.io.FilterReader
public void mark(int readAheadLimit)
throws java.io.IOException
mark in class java.io.FilterReader
public boolean ready()
throws java.io.IOException
ready in class java.io.FilterReader
public void skip(int n)
throws java.io.IOException
|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||