Class DummiesIO

java.lang.Object
  extended byDummiesIO

public class DummiesIO
extends java.lang.Object

This class implments simple text-based input and output for the keyboard and for disk files. For examples of the use of methods in this class, see Beginning Programming with For Dummies by Barry Burd, ©2003 John Wiley & Sons, Inc.

Most of the methods in this class are getType methods, print methods, or println methods. For instance, to read an int value from the keyboard, call

   intVariable = DummiesIO.getInt();
To read an int value from a file named myFile.txt, call
   intVariable = DummiesIO.getInt("myFile.txt");

A disk file's name can be relative to the directory containing the code (as in "myFile.txt"). Alternatively, the file name can be absolute, as in

   intVariable = DummiesIO.getInt("c:\\JavaPrograms\\myFiletxt");
For absolute file names in Windows, be sure to use two backslashes in place of each single backslash (as file separators).

(Note: Supplying file name strings to input/output methods is unique to this DummiesIO program. With real, industrial-strength IO programs, you generally don't use file-name strings to refer to files.)

To read several values, one after another, separate the values with any number of blank spaces and/or any number of newlines. For instance, to read the number 23 followed by the word Hello from the keyboard, use the following Java code:

   intVariable = DummiesIO.getInt();
   stringVariable = DummiesIO.getString();
You can enter the input on the keyboard in any of the following ways:
23 Hello

23     Hello

     23


    Hello

23
 Hello

To write the value 77 to a file named myOutput.txt, call
   DummiesIO.print("myOutput.txt", 77);
To write the value 77 to the same file, and to go to the next line after writing that value, call
   DummiesIO.println("myOutput.txt", 77);
The latter call to println is equivalent to the following two calls:
   DummiesIO.print("myOutput.txt", 77);
   DummiesIO.println("myOutput.txt");


Method Summary
static boolean endOfFile(java.lang.String fileName)
          Tests to see if you've reached the end of a particular disk file.
static boolean getBoolean()
          Reads a boolean value from the keyboard.
static boolean getBoolean(java.lang.String fileName)
          Reads a boolean value from a disk file
static char getChar()
          Reads a char value from the keyboard.
static char getChar(java.lang.String fileName)
          Reads a char value from a disk file
static double getDouble()
          Reads a double value from the keyboard.
static double getDouble(java.lang.String fileName)
          Reads a double value from a disk file
static float getFloat()
          Reads a float value from the keyboard.
static float getFloat(java.lang.String fileName)
          Reads a float value from a disk file
static int getInt()
          Reads an int value from the keyboard.
static int getInt(java.lang.String fileName)
          Reads an int value from a disk file
static java.lang.String getLine()
          Reads the remaining line (up to the pressing of the Enter key) from the keyboard.
static java.lang.String getLine(java.lang.String fileName)
          Reads the remaining line (up to the start of a new line) from a disk file.
static long getLong()
          Reads a long value from the keyboard.
static long getLong(java.lang.String fileName)
          Reads a long value from a disk file
static short getShort()
          Reads a short value from the keyboard.
static short getShort(java.lang.String fileName)
          Reads a short value from a disk file
static java.lang.String getString()
          Reads a string of characters from the keyboard.
static java.lang.String getString(java.lang.String fileName)
          Reads a string of characters (up to the next blank space or the end of a line) from a disk file
static void print(java.lang.String fileName, boolean value)
          Writes the word true or the word false to a disk file.
static void print(java.lang.String fileName, char value)
          Writes a single character (without single quote marks) to a disk file.
static void print(java.lang.String fileName, double value)
          Writes a double value to a disk file.
static void print(java.lang.String fileName, float value)
          Writes a float value to a disk file.
static void print(java.lang.String fileName, int value)
          Writes an int value to a disk file.
static void print(java.lang.String fileName, long value)
          Writes a long to a disk file.
static void print(java.lang.String fileName, java.lang.String value)
          Writes a string of characters to a disk file.
static void println(java.lang.String fileName)
          Writes no visible text to a disk file, but goes to the next line in the disk file (in preparation for any subsequent print or println calls that write to this file).
static void println(java.lang.String fileName, boolean value)
          Writes the word true or the word false to a disk file.
static void println(java.lang.String fileName, char value)
          Writes a single character (without single quote marks) to a disk file.
static void println(java.lang.String fileName, double value)
          Writes a double value to a disk file.
static void println(java.lang.String fileName, float value)
          Writes a float value to a disk file.
static void println(java.lang.String fileName, int value)
          Writes an int value to a disk file.
static void println(java.lang.String fileName, long value)
          Writes a long to a disk file.
static void println(java.lang.String fileName, java.lang.String value)
          Writes a string of characters to a disk file.
static void setForgiving(boolean value)
          With forgiving set to true, DummiesIO is generally more willing to continue running when it encounters an IO error.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

setForgiving

public static void setForgiving(boolean value)
With forgiving set to true, DummiesIO is generally more willing to continue running when it encounters an IO error. For example, if you call getInt(), the user types a letter rather than an integer, and forgiving is true, then the call to getInt() returns the value 0, and the program continues to run. By default, forgiving is false.

Parameters:
value - Either true for forgiving, or false for unforgiving

endOfFile

public static boolean endOfFile(java.lang.String fileName)
Tests to see if you've reached the end of a particular disk file. If so, the method returns true. Otherwise the method returns false. For instance, to find out if you've read up to the very end of myFile.txt check
   if (DummiesIO.endOfFile("myFile.txt")) ... Etc.

Parameters:
fileName - The name of a file
Returns:
true if you've reached the end of the file, and false otherwise

getInt

public static int getInt()
Reads an int value from the keyboard. At the keyboard, the user types a sequence of digits, optionally preceded by a minus sign.

Returns:
The int value typed on the keyboard by the user
See Also:
getInt(java.lang.String)

getInt

public static int getInt(java.lang.String fileName)
Reads an int value from a disk file

Parameters:
fileName - The name of a file
Returns:
The int value found in the file
See Also:
getInt()

getDouble

public static double getDouble()
Reads a double value from the keyboard. At the keyboard, the user types a double literal. Examples of double literals include
   1e1   2.   .3   0.0   3.14   1e-9d

Returns:
The double value typed on the keyboard by the user
See Also:
getDouble(java.lang.String)

getDouble

public static double getDouble(java.lang.String fileName)
Reads a double value from a disk file

Parameters:
fileName - The name of a file
Returns:
The double value found in the file
See Also:
getDouble()

getChar

public static char getChar()
Reads a char value from the keyboard. At the keyboard, the user types a single character. The character can be a letter, a digit, a punctuation symbol, or whatever. In typing the character, the user does not surround the character with quote marks (as you would in typing a character literal in a Java program).

Unlike other DummiesIO.get methods, the getChar method does not expect blank spaces to separate one value from another. For instance, the following code

   char1 = DummiesIO.getChar();
   char2 = DummiesIO.getChar();
   char3 = DummiesIO.getChar();
with the following input
   xyz
will put x in the variable char1, y in the variable char2, and z in the variable char3. With the following (slightly different) input
   x y z
and the same three calls to getChar, the value of char1 will be x, but char2 will contain a blank space, and the value of char3 will be y.

Returns:
The char value typed on the keyboard by the user
See Also:
getChar(java.lang.String)

getChar

public static char getChar(java.lang.String fileName)
Reads a char value from a disk file

Parameters:
fileName - The name of a file
Returns:
The char value found in the file
See Also:
getChar()

getBoolean

public static boolean getBoolean()
Reads a boolean value from the keyboard. Returns true if the user types true; returns false otherwise. The input is not case sensitive. (The method returns true if the user types tRuE.)

Returns:
The boolean value typed on the keyboard by the user
See Also:
getBoolean(java.lang.String)

getBoolean

public static boolean getBoolean(java.lang.String fileName)
Reads a boolean value from a disk file

Parameters:
fileName - The name of a file
Returns:
The boolean value found in the file
See Also:
getBoolean()

getString

public static java.lang.String getString()
Reads a string of characters from the keyboard. At the keyboard, the user types a sequence of characters followed by a blank space or by the end of a line. In typing the sequence of characters, the user does not surround these characters with quote marks (as you would in typing a String literal in a Java program).

Returns:
The next sequence of characters typed on the keyboard by the user (up to but not including the first blank space or the end of a line)
See Also:
getString(java.lang.String), getLine(), getLine(java.lang.String)

getString

public static java.lang.String getString(java.lang.String fileName)
Reads a string of characters (up to the next blank space or the end of a line) from a disk file

Parameters:
fileName - The name of a file
Returns:
The next sequence of characters from a disk file (up to but not including the first blank space or the end of a line)
See Also:
getString()

getShort

public static short getShort()
Reads a short value from the keyboard. At the keyboard, the user types a sequence of digits, optionally preceded by a minus sign.

Returns:
The short value typed on the keyboard by the user
See Also:
getShort(java.lang.String)

getShort

public static short getShort(java.lang.String fileName)
Reads a short value from a disk file

Parameters:
fileName - The name of a file
Returns:
The short value found in the file
See Also:
getShort()

getLong

public static long getLong()
Reads a long value from the keyboard. At the keyboard, the user types a sequence of digits, optionally preceded by a minus sign. The user does not type the letter L or l, as you would for a long literal in a Java program.

Returns:
The long value typed on the keyboard by the user
See Also:
getLong(java.lang.String)

getLong

public static long getLong(java.lang.String fileName)
Reads a long value from a disk file

Parameters:
fileName - The name of a file
Returns:
The long value found in the file
See Also:
getLong()

getFloat

public static float getFloat()
Reads a float value from the keyboard. At the keyboard, the user types a float literal. Examples of float literals include
   1e1f   2.f   .3f   0f   3.14f   6.022137e+23f

Returns:
The float value typed on the keyboard by the user
See Also:
getFloat(java.lang.String)

getFloat

public static float getFloat(java.lang.String fileName)
Reads a float value from a disk file

Parameters:
fileName - The name of a file
Returns:
The float value found in the file
See Also:
getFloat()

getLine

public static java.lang.String getLine()
Reads the remaining line (up to the pressing of the Enter key) from the keyboard. This method reads past blank spaces. For instance, if the user types
   Hello, how are you?
then one call to the getLine method gets all four words and the two punctuation symbols.

This method doesn't necessarily read an entire line from beginning to end. Instead, the method can read from a point in the middle of a line to the end of the line. For example, if the user types the following:

   42 Hello there!
   Goodbye! 88
and the Java program calls
   x = getInt();
   y = getLine();
   z = getLine();
then the value of x is 42, the value of y is "Hello there!", and the value of z is "Goodbye! 88".

Returns:
The next sequence of characters typed on the keyboard by the user (up to the user's pressing of the Enter key)
See Also:
getLine(java.lang.String), getString(), getString(java.lang.String)

getLine

public static java.lang.String getLine(java.lang.String fileName)
Reads the remaining line (up to the start of a new line) from a disk file.

Parameters:
fileName - The name of a file
Returns:
The next sequence of characters found in the file (up to the end of the current line)
See Also:
getLine()

println

public static void println(java.lang.String fileName,
                           boolean value)
Writes the word true or the word false to a disk file. After writing the word, this method goes to the next line in the disk file (in preparation for any subsequent print or println calls that write to this file).

Parameters:
fileName - The name of a file
value - The boolean value that's to be written to the file
See Also:
print(java.lang.String, boolean)

print

public static void print(java.lang.String fileName,
                         boolean value)
Writes the word true or the word false to a disk file. After writing the word, this method remains on the same line in the disk file (in preparation for any subsequent print or println calls that write to this file).

Parameters:
fileName - The name of a file
value - The boolean value that's to be written to the file
See Also:
println(java.lang.String, boolean)

println

public static void println(java.lang.String fileName,
                           char value)
Writes a single character (without single quote marks) to a disk file. After writing the character, this method goes to the next line in the disk file (in preparation for any subsequent print or println calls that write to this file).

Parameters:
fileName - The name of a file
value - The char value that's to be written to the file
See Also:
print(java.lang.String, char)

print

public static void print(java.lang.String fileName,
                         char value)
Writes a single character (without single quote marks) to a disk file. After writing the character, this method remains on the same line in the disk file (in preparation for any subsequent print or println calls that write to this file).

Parameters:
fileName - The name of a file
value - The char value that's to be written to the file
See Also:
println(java.lang.String, char)

println

public static void println(java.lang.String fileName,
                           double value)
Writes a double value to a disk file. After writing the value, this method goes to the next line in the disk file (in preparation for any subsequent print or println calls that write to this file).

Parameters:
fileName - The name of a file
value - The double value that's to be written to the file
See Also:
print(java.lang.String, double)

print

public static void print(java.lang.String fileName,
                         double value)
Writes a double value to a disk file. After writing the value, this method remains on the same line in the disk file (in preparation for any subsequent print or println calls that write to this file).

Parameters:
fileName - The name of a file
value - The double value that's to be written to the file
See Also:
println(java.lang.String, double)

println

public static void println(java.lang.String fileName,
                           float value)
Writes a float value to a disk file. After writing the value, this method goes to the next line in the disk file (in preparation for any subsequent print or println calls that write to this file).

Parameters:
fileName - The name of a file
value - The float value that's to be written to the file
See Also:
print(java.lang.String, float)

print

public static void print(java.lang.String fileName,
                         float value)
Writes a float value to a disk file. After writing the value, this method remains on the same line in the disk file (in preparation for any subsequent print or println calls that write to this file).

Parameters:
fileName - The name of a file
value - The float value that's to be written to the file
See Also:
println(java.lang.String, float)

println

public static void println(java.lang.String fileName,
                           int value)
Writes an int value to a disk file. After writing the value, this method goes to the next line in the disk file (in preparation for any subsequent print or println calls that write to this file).

Parameters:
fileName - The name of a file
value - The int value that's to be written to the file
See Also:
print(java.lang.String, int)

print

public static void print(java.lang.String fileName,
                         int value)
Writes an int value to a disk file. After writing the value, this method remains on the same line in the disk file (in preparation for any subsequent print or println calls that write to this file).

Parameters:
fileName - The name of a file
value - The int value that's to be written to the file
See Also:
println(java.lang.String, int)

println

public static void println(java.lang.String fileName,
                           long value)
Writes a long to a disk file. After writing the value, this method goes to the next line in the disk file (in preparation for any subsequent print or println calls that write to this file).

Parameters:
fileName - The name of a file
value - The long value that's to be written to the file
See Also:
print(java.lang.String, long)

print

public static void print(java.lang.String fileName,
                         long value)
Writes a long to a disk file. After writing the value, this method remains on the same line in the disk file (in preparation for any subsequent print or println calls that write to this file).

Parameters:
fileName - The name of a file
value - The long value that's to be written to the file
See Also:
println(java.lang.String, long)

println

public static void println(java.lang.String fileName,
                           java.lang.String value)
Writes a string of characters to a disk file. This method does not add double quote marks to the beginning and end of the string (and for most purposes, there's no reason to surround the string with double quote marks). After writing the string, this method goes to the next line in the disk file (in preparation for any subsequent print or println calls that write to this file).

Parameters:
fileName - The name of a file
value - The String value that's to be written to the file
See Also:
print(java.lang.String, java.lang.String)

print

public static void print(java.lang.String fileName,
                         java.lang.String value)
Writes a string of characters to a disk file. This method does not add double quote marks to the beginning and end of the string (and for most purposes, there's no reason to surround the string with double quote marks). After writing the value, this method remains on the same line in the disk file (in preparation for any subsequent print or println calls that write to this file).

Parameters:
fileName - The name of a file
value - The String value that's to be written to the file
See Also:
println(java.lang.String, java.lang.String)

println

public static void println(java.lang.String fileName)
Writes no visible text to a disk file, but goes to the next line in the disk file (in preparation for any subsequent print or println calls that write to this file).

Parameters:
fileName - The name of a file