public class TextIO extends java.lang.Object
For writing to standard output, the output methods in this class pretty much duplicate the functionality of System.out, and System.out can be used interchangeably with them.
This class does not use optimal Java programming practices. It is designed specifically to be easily usable even by a beginning programmer who has not yet learned about objects and exceptions. Therefore, everything is in a single source file that compiles into a single class file, all the methods are static methods, and none of the methods throw exceptions that would require try...catch statements. Also for this reason, all exceptions are converted into IllegalArgumentExceptions, even when this exception type doesn't really make sense.
This class requires Java 5.0 or higher. (A previous version of TextIO required only Java 1.1; this version should work with any source code that used the previous version, but it has some new features, including the type of formatted output that was introduced in Java 5 and the ability to use files and streams.)
Modifier and Type | Field and Description |
---|---|
static char |
EOF
The value returned by the peek() method when the input is at end-of-file.
|
static char |
EOLN
The value returned by the peek() method when the input is at end-of-line.
|
Constructor and Description |
---|
TextIO() |
Modifier and Type | Method and Description |
---|---|
static boolean |
eof()
Test whether the next character in the current input source is an end-of-file.
|
static boolean |
eoln()
Test whether the next character in the current input source is an end-of-line.
|
static char |
getAnyChar()
Reads the next character from the current input source.
|
static boolean |
getBoolean()
Skips whitespace characters and then reads a value of type boolean from input.
|
static byte |
getByte()
Skips whitespace characters and then reads a value of type byte from input.
|
static char |
getChar()
Skips whitespace characters and then reads a single non-whitespace character from input.
|
static double |
getDouble()
Skips whitespace characters and then reads a value of type double from input.
|
static float |
getFloat()
Skips whitespace characters and then reads a value of type float from input.
|
static java.lang.String |
getInputFileName()
If TextIO is currently reading from a file, then the return value is the name of the file.
|
static int |
getInt()
Skips whitespace characters and then reads a value of type int from input.
|
static java.lang.String |
getln()
Reads all the characters from the current input source, up to the next end-of-line.
|
static boolean |
getlnBoolean()
Skips whitespace characters and then reads a value of type boolean from input, discarding the rest of
the current line of input (including the next end-of-line character, if any).
|
static byte |
getlnByte()
Skips whitespace characters and then reads a value of type byte from input, discarding the rest of
the current line of input (including the next end-of-line character, if any).
|
static char |
getlnChar()
Skips whitespace characters and then reads a value of type char from input, discarding the rest of
the current line of input (including the next end-of-line character, if any).
|
static double |
getlnDouble()
Skips whitespace characters and then reads a value of type double from input, discarding the rest of
the current line of input (including the next end-of-line character, if any).
|
static float |
getlnFloat()
Skips whitespace characters and then reads a value of type float from input, discarding the rest of
the current line of input (including the next end-of-line character, if any).
|
static int |
getlnInt()
Skips whitespace characters and then reads a value of type int from input, discarding the rest of
the current line of input (including the next end-of-line character, if any).
|
static long |
getlnLong()
Skips whitespace characters and then reads a value of type long from input, discarding the rest of
the current line of input (including the next end-of-line character, if any).
|
static short |
getlnShort()
Skips whitespace characters and then reads a value of type short from input, discarding the rest of
the current line of input (including the next end-of-line character, if any).
|
static java.lang.String |
getlnString()
This is identical to getln().
|
static java.lang.String |
getlnWord()
Skips whitespace characters and then reads one "word" from input, discarding the rest of
the current line of input (including the next end-of-line character, if any).
|
static long |
getLong()
Skips whitespace characters and then reads a value of type long from input.
|
static java.lang.String |
getOutputFileName()
If TextIO is currently writing to a file, then the return value is the name of the file.
|
static short |
getShort()
Skips whitespace characters and then reads a value of type short from input.
|
static java.lang.String |
getWord()
Skips whitespace characters and then reads one "word" from input.
|
static char |
peek()
Returns the next character in the current input source, without actually removing that
character from the input.
|
static void |
put(java.lang.Object x)
Write a single value to the current output destination, using the default format
and no extra spaces.
|
static void |
put(java.lang.Object x,
int minChars)
Write a single value to the current output destination, using the default format
and outputting at least minChars characters (with extra spaces added before the
output value if necessary).
|
static void |
putf(java.lang.String format,
java.lang.Object... items)
Writes formatted output values to the current output destination.
|
static void |
putln()
Write an end-of-line character to the current output destination.
|
static void |
putln(java.lang.Object x)
This is equivalent to put(x), followed by an end-of-line.
|
static void |
putln(java.lang.Object x,
int minChars)
This is equivalent to put(x,minChars), followed by an end-of-line.
|
static void |
readFile(java.lang.String fileName)
Opens a file with a specified name for input.
|
static void |
readStandardInput()
After this method is called, input will be read from standard input (as it
is in the default state).
|
static void |
readStream(java.io.InputStream inputStream)
After this method is called, input will be read from inputStream, provided it
is non-null.
|
static void |
readStream(java.io.Reader inputStream)
After this method is called, input will be read from inputStream, provided it
is non-null.
|
static boolean |
readUserSelectedFile()
Puts a GUI file-selection dialog box on the screen in which the user can select
an input file.
|
static void |
skipBlanks()
Skips over any whitespace characters, except for end-of-lines.
|
static void |
writeFile(java.lang.String fileName)
Opens a file with a specified name for output.
|
static void |
writeStandardOutput()
After this method is called, output will be written to standard output (as it
is in the default state).
|
static void |
writeStream(java.io.OutputStream outputStream)
After this method is called, output will be sent to outputStream, provided it
is non-null.
|
static void |
writeStream(java.io.PrintWriter outputStream)
After this method is called, output will be sent to outputStream, provided it
is non-null.
|
static boolean |
writeUserSelectedFile()
Puts a GUI file-selection dialog box on the screen in which the user can select
an output file.
|
public static final char EOF
public static final char EOLN
public static boolean eof()
public static boolean eoln()
public static char getAnyChar()
public static boolean getBoolean()
Legal inputs for a boolean input are: true, t, yes, y, 1, false, f, no, n, and 0; letters can be either upper case or lower case. One "word" of input is read, using the getWord() method, and it must be one of these; note that the "word" must be terminated by a whitespace character (or end-of-file).
public static byte getByte()
public static char getChar()
public static double getDouble()
public static float getFloat()
public static java.lang.String getInputFileName()
public static int getInt()
public static java.lang.String getln()
public static boolean getlnBoolean()
Legal inputs for a boolean input are: true, t, yes, y, 1, false, f, no, n, and 0; letters can be either upper case or lower case. One "word" of input is read, using the getWord() method, and it must be one of these; note that the "word" must be terminated by a whitespace character (or end-of-file).
public static byte getlnByte()
public static char getlnChar()
public static double getlnDouble()
public static float getlnFloat()
public static int getlnInt()
public static long getlnLong()
public static short getlnShort()
public static java.lang.String getlnString()
public static java.lang.String getlnWord()
public static long getLong()
public static java.lang.String getOutputFileName()
public static short getShort()
public static java.lang.String getWord()
public static char peek()
public static void put(java.lang.Object x)
public static void put(java.lang.Object x, int minChars)
x
- The value to be output, which can be of any type.minChars
- The minimum number of characters to use for the output. If x requires fewer
then this number of characters, then extra spaces are added to the front of x to bring
the total up to minChars. If minChars is less than or equal to zero, then x will be printed
in the minimum number of spaces possible.public static void putf(java.lang.String format, java.lang.Object... items)
public static void putln()
public static void putln(java.lang.Object x)
public static void putln(java.lang.Object x, int minChars)
public static void readFile(java.lang.String fileName)
public static void readStandardInput()
public static void readStream(java.io.InputStream inputStream)
public static void readStream(java.io.Reader inputStream)
public static boolean readUserSelectedFile()
NOTE: Calling this method starts a GUI user interface thread, which can continue to run even if the thread that runs the main program ends. If you use this method in a non-GUI program, it might be necessary to call System.exit(0) at the end of the main() routine to shut down the Java virtual machine completely.
public static void skipBlanks()
public static void writeFile(java.lang.String fileName)
NOTE: Calling this method starts a GUI user interface thread, which can continue to run even if the thread that runs the main program ends. If you use this method in a non-GUI program, it might be necessary to call System.exit(0) at the end of the main() routine to shut down the Java virtual machine completely.
public static void writeStandardOutput()
public static void writeStream(java.io.OutputStream outputStream)
public static void writeStream(java.io.PrintWriter outputStream)
public static boolean writeUserSelectedFile()