Java  1.0
Static Public Member Functions | Static Public Attributes | Static Private Member Functions | Static Private Attributes | List of all members
TextIO Class Reference

TextIO provides a set of static methods for reading and writing text. More...

Collaboration diagram for TextIO:
Collaboration graph
[legend]

Static Public Member Functions

static void readStandardInput ()
 After this method is called, input will be read from standard input (as it is in the default state). More...
 
static void readStream (InputStream inputStream)
 After this method is called, input will be read from inputStream, provided it is non-null. More...
 
static void readStream (Reader inputStream)
 After this method is called, input will be read from inputStream, provided it is non-null. More...
 
static void readFile (String fileName)
 Opens a file with a specified name for input. More...
 
static boolean readUserSelectedFile ()
 Puts a GUI file-selection dialog box on the screen in which the user can select an input file. More...
 
static void writeStandardOutput ()
 After this method is called, output will be written to standard output (as it is in the default state). More...
 
static void writeStream (OutputStream outputStream)
 After this method is called, output will be sent to outputStream, provided it is non-null. More...
 
static void writeStream (PrintWriter outputStream)
 After this method is called, output will be sent to outputStream, provided it is non-null. More...
 
static void writeFile (String fileName)
 Opens a file with a specified name for output. More...
 
static boolean writeUserSelectedFile ()
 Puts a GUI file-selection dialog box on the screen in which the user can select an output file. More...
 
static String getInputFileName ()
 If TextIO is currently reading from a file, then the return value is the name of the file. More...
 
static String getOutputFileName ()
 If TextIO is currently writing to a file, then the return value is the name of the file. More...
 
static void put (Object x)
 Write a single value to the current output destination, using the default format and no extra spaces. More...
 
static void put (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). More...
 
static void putln (Object x)
 This is equivalent to put(x), followed by an end-of-line. More...
 
static void putln (Object x, int minChars)
 This is equivalent to put(x,minChars), followed by an end-of-line. More...
 
static void putln ()
 Write an end-of-line character to the current output destination. More...
 
static void putf (String format, Object... items)
 Writes formatted output values to the current output destination. More...
 
static boolean eoln ()
 Test whether the next character in the current input source is an end-of-line. More...
 
static boolean eof ()
 Test whether the next character in the current input source is an end-of-file. More...
 
static char getAnyChar ()
 Reads the next character from the current input source. More...
 
static char peek ()
 Returns the next character in the current input source, without actually removing that character from the input. More...
 
static void skipBlanks ()
 Skips over any whitespace characters, except for end-of-lines. More...
 
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). More...
 
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). More...
 
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). More...
 
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). More...
 
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). More...
 
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). More...
 
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). More...
 
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). More...
 
static 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). More...
 
static String getlnString ()
 This is identical to getln(). More...
 
static String getln ()
 Reads all the characters from the current input source, up to the next end-of-line. More...
 
static byte getByte ()
 Skips whitespace characters and then reads a value of type byte from input. More...
 
static short getShort ()
 Skips whitespace characters and then reads a value of type short from input. More...
 
static int getInt ()
 Skips whitespace characters and then reads a value of type int from input. More...
 
static long getLong ()
 Skips whitespace characters and then reads a value of type long from input. More...
 
static char getChar ()
 Skips whitespace characters and then reads a single non-whitespace character from input. More...
 
static float getFloat ()
 Skips whitespace characters and then reads a value of type float from input. More...
 
static double getDouble ()
 Skips whitespace characters and then reads a value of type double from input. More...
 
static String getWord ()
 Skips whitespace characters and then reads one "word" from input. More...
 
static boolean getBoolean ()
 Skips whitespace characters and then reads a value of type boolean from input. More...
 

Static Public Attributes

static final char EOF = (char)0xFFFF
 The value returned by the peek() method when the input is at end-of-file. More...
 
static final char EOLN = '\n'
 The value returned by the peek() method when the input is at end-of-line. More...
 

Static Private Member Functions

static void skipWhitespace ()
 Skips over any whitespace characters, including for end-of-lines. More...
 
static String readRealString ()
 
static String readIntegerString ()
 
static long readInteger (long min, long max)
 
static void errorMessage (String message, String expecting)
 
static char lookChar ()
 
static char readChar ()
 
static void fillBuffer ()
 
static void emptyBuffer ()
 
static void outputError (String message)
 

Static Private Attributes

static String inputFileName
 
static String outputFileName
 
static JFileChooser fileDialog
 
static final BufferedReader standardInput = new BufferedReader(new InputStreamReader(System.in))
 
static final PrintWriter standardOutput = new PrintWriter(System.out)
 
static BufferedReader in = standardInput
 
static PrintWriter out = standardOutput
 
static boolean readingStandardInput = true
 
static boolean writingStandardOutput = true
 
static int inputErrorCount
 
static int outputErrorCount
 
static Matcher integerMatcher
 
static Matcher floatMatcher
 
static final Pattern integerRegex = Pattern.compile("(\\+|-)?[0-9]+")
 
static final Pattern floatRegex = Pattern.compile("(\\+|-)?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))((e|E)(\\+|-)?[0-9]+)?")
 
static String buffer = null
 
static int pos = 0
 

Detailed Description

TextIO provides a set of static methods for reading and writing text.

By default, it reads from standard input and writes to standard output, but it is possible to redirect the input and output to files or to other input and output streams. When the standard input and output streams are being used, the input methods will not produce an error; instead, the user is repeatedly prompted for input until a legal input is entered. (If standard input has been changed externally, as by file redirection on the command line, this is not a reasonable behavior; to handle this case, TextIO will give up after 10 consecutive illegal inputs and will throw an IllegalArgumentException.) For the most part, any other error will be translated into an IllegalArguementException.

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

Definition at line 34 of file TextIO.java.

Member Function Documentation

◆ emptyBuffer()

static void TextIO.emptyBuffer ( )
staticprivate

◆ eof()

static boolean TextIO.eof ( )
static

Test whether the next character in the current input source is an end-of-file.

Note that this method does NOT skip whitespace before testing for end-of-line – if you want to do that, call skipBlanks() or skipWhitespace() first.

Definition at line 459 of file TextIO.java.

References EOF, and peek().

◆ eoln()

static boolean TextIO.eoln ( )
static

Test whether the next character in the current input source is an end-of-line.

Note that this method does NOT skip whitespace before testing for end-of-line – if you want to do that, call skipBlanks() first.

Definition at line 450 of file TextIO.java.

References peek().

◆ errorMessage()

static void TextIO.errorMessage ( String  message,
String  expecting 
)
staticprivate

◆ fillBuffer()

static void TextIO.fillBuffer ( )
staticprivate

Definition at line 969 of file TextIO.java.

References buffer, floatMatcher, in, inputFileName, integerMatcher, pos, and readingStandardInput.

Referenced by lookChar().

◆ getAnyChar()

static char TextIO.getAnyChar ( )
static

Reads the next character from the current input source.

The character can be a whitespace character; compare this to the getChar() method, which skips over whitespace and returns the next non-whitespace character. An end-of-line is always returned as the character '\n', even when the actual end-of-line in the input source is something else, such as '\r' or "\r\n". This method will throw an IllegalArgumentException if the input is at end-of-file (which will not ordinarily happen if reading from standard input).

Definition at line 471 of file TextIO.java.

References readChar().

Referenced by roman.main().

◆ getBoolean()

static boolean TextIO.getBoolean ( )
static

Skips whitespace characters and then reads a value of type boolean from input.

Any additional characters on the current line of input are retained, and will be read by the next input operation. When using standard IO, this will not produce an error; the user will be prompted repeatedly for input until a legal value is input. In other cases, an IllegalArgumentException will be thrown if a legal value is not found.

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

Definition at line 803 of file TextIO.java.

References errorMessage(), getWord(), and inputErrorCount.

Referenced by getlnBoolean().

◆ getByte()

static byte TextIO.getByte ( )
static

Skips whitespace characters and then reads a value of type byte from input.

Any additional characters on the current line of input are retained, and will be read by the next input operation. When using standard IO, this will not produce an error; the user will be prompted repeatedly for input until a legal value is input. In other cases, an IllegalArgumentException will be thrown if a legal value is not found.

Definition at line 661 of file TextIO.java.

References readInteger().

Referenced by getlnByte().

◆ getChar()

static char TextIO.getChar ( )
static

Skips whitespace characters and then reads a single non-whitespace character from input.

Any additional characters on the current line of input are retained, and will be read by the next input operation. When using standard IO, this will not produce an error. In other cases, an IllegalArgumentException will be thrown if an end-of-file is encountered.

Definition at line 701 of file TextIO.java.

References readChar(), and skipWhitespace().

Referenced by getlnChar().

◆ getDouble()

static double TextIO.getDouble ( )
static

Skips whitespace characters and then reads a value of type double from input.

Any additional characters on the current line of input are retained, and will be read by the next input operation. When using standard IO, this will not produce an error; the user will be prompted repeatedly for input until a legal value is input. In other cases, an IllegalArgumentException will be thrown if a legal value is not found.

Definition at line 747 of file TextIO.java.

References errorMessage(), inputErrorCount, and readRealString().

Referenced by getlnDouble().

◆ getFloat()

static float TextIO.getFloat ( )
static

Skips whitespace characters and then reads a value of type float from input.

Any additional characters on the current line of input are retained, and will be read by the next input operation. When using standard IO, this will not produce an error; the user will be prompted repeatedly for input until a legal value is input. In other cases, an IllegalArgumentException will be thrown if a legal value is not found.

Definition at line 712 of file TextIO.java.

References errorMessage(), inputErrorCount, and readRealString().

Referenced by getlnFloat().

◆ getInputFileName()

static String TextIO.getInputFileName ( )
static

If TextIO is currently reading from a file, then the return value is the name of the file.


If the class is reading from standard input or from a stream, then the return value is null.

Definition at line 340 of file TextIO.java.

References inputFileName.

◆ getInt()

static int TextIO.getInt ( )
static

Skips whitespace characters and then reads a value of type int from input.

Any additional characters on the current line of input are retained, and will be read by the next input operation. When using standard IO, this will not produce an error; the user will be prompted repeatedly for input until a legal value is input. In other cases, an IllegalArgumentException will be thrown if a legal value is not found.

Definition at line 681 of file TextIO.java.

References readInteger().

Referenced by getlnInt().

◆ getln()

static String TextIO.getln ( )
static

Reads all the characters from the current input source, up to the next end-of-line.

The end-of-line is read but is not included in the return value. Any other whitespace characters on the line are retained, even if they occur at the start of input. The return value will be an empty string if there are no no characters before the end-of-line. When using standard IO, this will not produce an error.
In other cases, an IllegalArgumentException will be thrown if an end-of-file is encountered.

Definition at line 645 of file TextIO.java.

References readChar().

Referenced by getlnString(), and roman.main().

◆ getlnBoolean()

static boolean TextIO.getlnBoolean ( )
static

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

When using standard IO, this will not produce an error; the user will be prompted repeatedly for input until a legal value is input. In other cases, an IllegalArgumentException will be thrown if a legal value is not found.

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

Definition at line 612 of file TextIO.java.

References emptyBuffer(), and getBoolean().

◆ getlnByte()

static byte TextIO.getlnByte ( )
static

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

When using standard IO, this will not produce an error; the user will be prompted repeatedly for input until a legal value is input. In other cases, an IllegalArgumentException will be thrown if a legal value is not found.

Definition at line 524 of file TextIO.java.

References emptyBuffer(), and getByte().

◆ getlnChar()

static char TextIO.getlnChar ( )
static

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

Note that the value that is returned will be a non-whitespace character; compare this with the getAnyChar() method. When using standard IO, this will not produce an error. In other cases, an error can occur if an end-of-file is encountered.

Definition at line 597 of file TextIO.java.

References emptyBuffer(), and getChar().

◆ getlnDouble()

static double TextIO.getlnDouble ( )
static

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

When using standard IO, this will not produce an error; the user will be prompted repeatedly for input until a legal value is input. In other cases, an IllegalArgumentException will be thrown if a legal value is not found.

Definition at line 584 of file TextIO.java.

References emptyBuffer(), and getDouble().

◆ getlnFloat()

static float TextIO.getlnFloat ( )
static

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

When using standard IO, this will not produce an error; the user will be prompted repeatedly for input until a legal value is input. In other cases, an IllegalArgumentException will be thrown if a legal value is not found.

Definition at line 572 of file TextIO.java.

References emptyBuffer(), and getFloat().

◆ getlnInt()

static int TextIO.getlnInt ( )
static

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

When using standard IO, this will not produce an error; the user will be prompted repeatedly for input until a legal value is input. In other cases, an IllegalArgumentException will be thrown if a legal value is not found.

Definition at line 548 of file TextIO.java.

References emptyBuffer(), and getInt().

Referenced by roman.main().

◆ getlnLong()

static long TextIO.getlnLong ( )
static

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

When using standard IO, this will not produce an error; the user will be prompted repeatedly for input until a legal value is input. In other cases, an IllegalArgumentException will be thrown if a legal value is not found.

Definition at line 560 of file TextIO.java.

References emptyBuffer(), and getLong().

◆ getlnShort()

static short TextIO.getlnShort ( )
static

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

When using standard IO, this will not produce an error; the user will be prompted repeatedly for input until a legal value is input. In other cases, an IllegalArgumentException will be thrown if a legal value is not found.

Definition at line 536 of file TextIO.java.

References emptyBuffer(), and getShort().

◆ getlnString()

static String TextIO.getlnString ( )
static

This is identical to getln().

Definition at line 634 of file TextIO.java.

References getln().

◆ getlnWord()

static String TextIO.getlnWord ( )
static

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

A word is defined as a sequence of non-whitespace characters (not just letters!). When using standard IO, this will not produce an error. In other cases, an IllegalArgumentException will be thrown if an end-of-file is encountered.

Definition at line 625 of file TextIO.java.

References emptyBuffer(), and getWord().

◆ getLong()

static long TextIO.getLong ( )
static

Skips whitespace characters and then reads a value of type long from input.

Any additional characters on the current line of input are retained, and will be read by the next input operation. When using standard IO, this will not produce an error; the user will be prompted repeatedly for input until a legal value is input. In other cases, an IllegalArgumentException will be thrown if a legal value is not found.

Definition at line 691 of file TextIO.java.

References readInteger().

Referenced by getlnLong().

◆ getOutputFileName()

static String TextIO.getOutputFileName ( )
static

If TextIO is currently writing to a file, then the return value is the name of the file.


If the class is writing to standard output or to a stream, then the return value is null.

Definition at line 349 of file TextIO.java.

References outputFileName.

◆ getShort()

static short TextIO.getShort ( )
static

Skips whitespace characters and then reads a value of type short from input.

Any additional characters on the current line of input are retained, and will be read by the next input operation. When using standard IO, this will not produce an error; the user will be prompted repeatedly for input until a legal value is input. In other cases, an IllegalArgumentException will be thrown if a legal value is not found.

Definition at line 671 of file TextIO.java.

References readInteger().

Referenced by getlnShort().

◆ getWord()

static String TextIO.getWord ( )
static

Skips whitespace characters and then reads one "word" from input.

Any additional characters on the current line of input are retained, and will be read by the next input operation. A word is defined as a sequence of non-whitespace characters (not just letters!). When using standard IO, this will not produce an error. In other cases, an IllegalArgumentException will be thrown if an end-of-file is encountered.

Definition at line 783 of file TextIO.java.

References EOF, lookChar(), readChar(), and skipWhitespace().

Referenced by getBoolean(), and getlnWord().

◆ lookChar()

static char TextIO.lookChar ( )
staticprivate

◆ outputError()

static void TextIO.outputError ( String  message)
staticprivate

Definition at line 990 of file TextIO.java.

References outputErrorCount, outputFileName, and writingStandardOutput.

Referenced by put(), putf(), and putln().

◆ peek()

static char TextIO.peek ( )
static

Returns the next character in the current input source, without actually removing that character from the input.

The character can be a whitespace character and can be the end-of-file character (specified by the constant TextIO.EOF).An end-of-line is always returned as the character '\n', even when the actual end-of-line in the input source is something else, such as '\r' or "\r\n". This method never causes an error.

Definition at line 482 of file TextIO.java.

References lookChar().

Referenced by eof(), eoln(), and roman.main().

◆ put() [1/2]

static void TextIO.put ( Object  x)
static

Write a single value to the current output destination, using the default format and no extra spaces.

This method will handle any type of parameter, even one whose type is one of the primitive types.

Definition at line 361 of file TextIO.java.

References out, and outputError().

Referenced by roman.main(), and putln().

◆ put() [2/2]

static void TextIO.put ( Object  x,
int  minChars 
)
static

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

This method will handle any type of parameter, even one whose type is one of the primitive types.

Parameters
xThe value to be output, which can be of any type.
minCharsThe 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 minumum number of spaces possible.

Definition at line 379 of file TextIO.java.

References out, and outputError().

◆ putf()

static void TextIO.putf ( String  format,
Object...  items 
)
static

Writes formatted output values to the current output destination.

This method has the same function as System.out.printf(); the details of formatted output are not discussed here. The first parameter is a string that describes the format of the output. There can be any number of additional parameters; these specify the values to be output and can be of any type. This method will throw an IllegalArgumentException if the format string is null or if the format string is illegal for the values that are being output.

Definition at line 429 of file TextIO.java.

References out, and outputError().

◆ putln() [1/3]

static void TextIO.putln ( )
static

Write an end-of-line character to the current output destination.

Definition at line 413 of file TextIO.java.

References out, and outputError().

◆ putln() [2/3]

static void TextIO.putln ( Object  x)
static

This is equivalent to put(x), followed by an end-of-line.

Definition at line 392 of file TextIO.java.

References out, and outputError().

Referenced by roman.main().

◆ putln() [3/3]

static void TextIO.putln ( Object  x,
int  minChars 
)
static

This is equivalent to put(x,minChars), followed by an end-of-line.

Definition at line 402 of file TextIO.java.

References out, outputError(), and put().

◆ readChar()

static char TextIO.readChar ( )
staticprivate

◆ readFile()

static void TextIO.readFile ( String  fileName)
static

Opens a file with a specified name for input.

If the file name is null, this has the same effect as calling readStandardInput(); that is, input will be read from standard input. If an error occurs while trying to open the file, an exception of type IllegalArgumentException is thrown, and the input source is not changed. If the file is opened successfully, then after this method is called, all of the input routines will read from the file, instead of from standard input.

Definition at line 117 of file TextIO.java.

References emptyBuffer(), in, inputErrorCount, inputFileName, readingStandardInput, and readStandardInput().

◆ readInteger()

static long TextIO.readInteger ( long  min,
long  max 
)
staticprivate

Definition at line 886 of file TextIO.java.

References errorMessage(), inputErrorCount, and readIntegerString().

Referenced by getByte(), getInt(), getLong(), and getShort().

◆ readIntegerString()

static String TextIO.readIntegerString ( )
staticprivate

Definition at line 870 of file TextIO.java.

References buffer, EOF, integerMatcher, integerRegex, lookChar(), pos, and skipWhitespace().

Referenced by readInteger().

◆ readRealString()

static String TextIO.readRealString ( )
staticprivate

Definition at line 854 of file TextIO.java.

References buffer, EOF, floatMatcher, floatRegex, lookChar(), pos, and skipWhitespace().

Referenced by getDouble(), and getFloat().

◆ readStandardInput()

static void TextIO.readStandardInput ( )
static

After this method is called, input will be read from standard input (as it is in the default state).

If a file or stream was previously the input source, that file or stream is closed.

Definition at line 59 of file TextIO.java.

References emptyBuffer(), in, inputErrorCount, inputFileName, readingStandardInput, and standardInput.

Referenced by readFile(), readStream(), and TextIOApplet.ProgRun.run().

◆ readStream() [1/2]

static void TextIO.readStream ( InputStream  inputStream)
static

After this method is called, input will be read from inputStream, provided it is non-null.

If inputStream is null, then this method has the same effect as calling readStandardInput(); that is, future input will come from the standard input stream.

Definition at line 80 of file TextIO.java.

References readStandardInput().

◆ readStream() [2/2]

static void TextIO.readStream ( Reader  inputStream)
static

After this method is called, input will be read from inputStream, provided it is non-null.

If inputStream is null, then this method has the same effect as calling readStandardInput(); that is, future input will come from the standard input stream.

Definition at line 93 of file TextIO.java.

References emptyBuffer(), in, inputErrorCount, inputFileName, readingStandardInput, and readStandardInput().

◆ readUserSelectedFile()

static boolean TextIO.readUserSelectedFile ( )
static

Puts a GUI file-selection dialog box on the screen in which the user can select an input file.

If the user cancels the dialog instead of selecting a file, it is not considered an error, but the return value of the subroutine is false. If the user does select a file, but there is an error while trying to open the file, then an exception of type IllegalArgumentException is thrown. Finally, if the user selects a file and it is successfully opened, then the return value of the subroutine is true, and the input routines will read from the file, instead of from standard input. If the user cancels, or if any error occurs, then the previous input source is not changed.

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.

Definition at line 159 of file TextIO.java.

References emptyBuffer(), fileDialog, in, inputErrorCount, inputFileName, and readingStandardInput.

◆ skipBlanks()

static void TextIO.skipBlanks ( )
static

Skips over any whitespace characters, except for end-of-lines.

After this method is called, the next input character is either an end-of-line, an end-of-file, or a non-whitespace character. This method never causes an error. (Ordinarily, end-of-file is not possible when reading from standard input.)

Definition at line 492 of file TextIO.java.

References EOF, lookChar(), and readChar().

◆ skipWhitespace()

static void TextIO.skipWhitespace ( )
staticprivate

Skips over any whitespace characters, including for end-of-lines.

After this method is called, the next input character is either an end-of-file or a non-whitespace character. This method never causes an error. (Ordinarily, end-of-file is not possible when reading from standard input.)

Definition at line 506 of file TextIO.java.

References EOF, lookChar(), out, readChar(), readingStandardInput, and writingStandardOutput.

Referenced by getChar(), getWord(), readIntegerString(), and readRealString().

◆ writeFile()

static void TextIO.writeFile ( String  fileName)
static

Opens a file with a specified name for output.

If the file name is null, this has the same effect as calling writeStandardOutput(); that is, output will be sent to standard output. If an error occurs while trying to open the file, an exception of type IllegalArgumentException is thrown. If the file is opened successfully, then after this method is called, all of the output routines will write to the file, instead of to standard output. If an error occurs, the output destination is not changed.

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.

Definition at line 254 of file TextIO.java.

References out, outputErrorCount, outputFileName, writeStandardOutput(), and writingStandardOutput.

◆ writeStandardOutput()

static void TextIO.writeStandardOutput ( )
static

After this method is called, output will be written to standard output (as it is in the default state).

If a file or stream was previously open for output, it will be closed.

Definition at line 195 of file TextIO.java.

References out, outputErrorCount, outputFileName, standardOutput, and writingStandardOutput.

Referenced by TextIOApplet.ProgRun.run(), writeFile(), and writeStream().

◆ writeStream() [1/2]

static void TextIO.writeStream ( OutputStream  outputStream)
static

After this method is called, output will be sent to outputStream, provided it is non-null.

If outputStream is null, then this method has the same effect as calling writeStandardOutput(); that is, future output will be sent to the standard output stream.

Definition at line 216 of file TextIO.java.

References writeStandardOutput().

◆ writeStream() [2/2]

static void TextIO.writeStream ( PrintWriter  outputStream)
static

After this method is called, output will be sent to outputStream, provided it is non-null.

If outputStream is null, then this method has the same effect as calling writeStandardOutput(); that is, future output will be sent to the standard output stream.

Definition at line 229 of file TextIO.java.

References out, outputErrorCount, outputFileName, writeStandardOutput(), and writingStandardOutput.

◆ writeUserSelectedFile()

static boolean TextIO.writeUserSelectedFile ( )
static

Puts a GUI file-selection dialog box on the screen in which the user can select an output file.

If the user cancels the dialog instead of selecting a file, it is not considered an error, but the return value of the subroutine is false. If the user does select a file, but there is an error while trying to open the file, then an exception of type IllegalArgumentException is thrown. Finally, if the user selects a file and it is successfully opened, then the return value of the subroutine is true, and the output routines will write to the file, instead of to standard output. If the user cancels, or if an error occurs, then the current output destination is not changed.

Definition at line 291 of file TextIO.java.

References fileDialog, out, outputErrorCount, outputFileName, and writingStandardOutput.

Member Data Documentation

◆ buffer

String TextIO.buffer = null
staticprivate

◆ EOF

final char TextIO.EOF = (char)0xFFFF
static

The value returned by the peek() method when the input is at end-of-file.

(The value of this constant is (char)0xFFFF.)

Definition at line 45 of file TextIO.java.

Referenced by eof(), getWord(), lookChar(), readIntegerString(), readRealString(), skipBlanks(), and skipWhitespace().

◆ EOLN

final char TextIO.EOLN = '\n'
static

The value returned by the peek() method when the input is at end-of-line.

The value of this constant is the character '\n'.

Definition at line 51 of file TextIO.java.

◆ fileDialog

JFileChooser TextIO.fileDialog
staticprivate

Definition at line 832 of file TextIO.java.

Referenced by readUserSelectedFile(), and writeUserSelectedFile().

◆ floatMatcher

Matcher TextIO.floatMatcher
staticprivate

Definition at line 847 of file TextIO.java.

Referenced by fillBuffer(), and readRealString().

◆ floatRegex

final Pattern TextIO.floatRegex = Pattern.compile("(\\+|-)?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))((e|E)(\\+|-)?[0-9]+)?")
staticprivate

Definition at line 849 of file TextIO.java.

Referenced by readRealString().

◆ in

BufferedReader TextIO.in = standardInput
staticprivate

◆ inputErrorCount

int TextIO.inputErrorCount
staticprivate

◆ inputFileName

String TextIO.inputFileName
staticprivate

◆ integerMatcher

Matcher TextIO.integerMatcher
staticprivate

Definition at line 846 of file TextIO.java.

Referenced by fillBuffer(), and readIntegerString().

◆ integerRegex

final Pattern TextIO.integerRegex = Pattern.compile("(\\+|-)?[0-9]+")
staticprivate

Definition at line 848 of file TextIO.java.

Referenced by readIntegerString().

◆ out

PrintWriter TextIO.out = standardOutput
staticprivate

◆ outputErrorCount

int TextIO.outputErrorCount
staticprivate

◆ outputFileName

String TextIO.outputFileName
staticprivate

◆ pos

int TextIO.pos = 0
staticprivate

Definition at line 852 of file TextIO.java.

Referenced by fillBuffer(), lookChar(), readChar(), readIntegerString(), and readRealString().

◆ readingStandardInput

boolean TextIO.readingStandardInput = true
staticprivate

◆ standardInput

final BufferedReader TextIO.standardInput = new BufferedReader(new InputStreamReader(System.in))
staticprivate

Definition at line 834 of file TextIO.java.

Referenced by readStandardInput().

◆ standardOutput

final PrintWriter TextIO.standardOutput = new PrintWriter(System.out)
staticprivate

Definition at line 835 of file TextIO.java.

Referenced by writeStandardOutput().

◆ writingStandardOutput

boolean TextIO.writingStandardOutput = true
staticprivate

The documentation for this class was generated from the following file: