|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.ObjectDummiesIO
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
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 |
public static void setForgiving(boolean value)
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
.
value
- Either true
for forgiving, or false
for unforgivingpublic static boolean endOfFile(java.lang.String fileName)
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.
fileName
- The name of a file
true
if you've reached the end of the file, and false
otherwisepublic static int getInt()
int
value from the keyboard. At the keyboard,
the user types a sequence of digits, optionally preceded by a minus
sign.
int
value typed on the keyboard by the usergetInt(java.lang.String)
public static int getInt(java.lang.String fileName)
int
value from a disk file
fileName
- The name of a file
int
value found in the filegetInt()
public static double getDouble()
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
double
value typed on the keyboard by the usergetDouble(java.lang.String)
public static double getDouble(java.lang.String fileName)
double
value from a disk file
fileName
- The name of a file
double
value found in the filegetDouble()
public static char getChar()
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
xyzwill 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 zand 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
.
char
value typed on the keyboard by the usergetChar(java.lang.String)
public static char getChar(java.lang.String fileName)
char
value from a disk file
fileName
- The name of a file
char
value found in the filegetChar()
public static boolean getBoolean()
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
.)
boolean
value typed on the keyboard by the usergetBoolean(java.lang.String)
public static boolean getBoolean(java.lang.String fileName)
boolean
value from a disk file
fileName
- The name of a file
boolean
value found in the filegetBoolean()
public static java.lang.String getString()
getString(java.lang.String)
,
getLine()
,
getLine(java.lang.String)
public static java.lang.String getString(java.lang.String fileName)
fileName
- The name of a file
getString()
public static short getShort()
short
value from the keyboard. At the keyboard,
the user types a sequence of digits, optionally preceded by a minus
sign.
short
value typed on the keyboard by the usergetShort(java.lang.String)
public static short getShort(java.lang.String fileName)
short
value from a disk file
fileName
- The name of a file
short
value found in the filegetShort()
public static long getLong()
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.
long
value typed on the keyboard by the usergetLong(java.lang.String)
public static long getLong(java.lang.String fileName)
long
value from a disk file
fileName
- The name of a file
long
value found in the filegetLong()
public static float getFloat()
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
float
value typed on the keyboard by the usergetFloat(java.lang.String)
public static float getFloat(java.lang.String fileName)
float
value from a disk file
fileName
- The name of a file
float
value found in the filegetFloat()
public static java.lang.String getLine()
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! 88and 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"
.
getLine(java.lang.String)
,
getString()
,
getString(java.lang.String)
public static java.lang.String getLine(java.lang.String fileName)
fileName
- The name of a file
getLine()
public static void println(java.lang.String fileName, boolean value)
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).
fileName
- The name of a filevalue
- The boolean
value that's to be written to the fileprint(java.lang.String, boolean)
public static void print(java.lang.String fileName, boolean value)
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).
fileName
- The name of a filevalue
- The boolean
value that's to be written to the fileprintln(java.lang.String, boolean)
public static void println(java.lang.String fileName, char value)
print
or println
calls
that write to this file).
fileName
- The name of a filevalue
- The char
value that's to be written to the fileprint(java.lang.String, char)
public static void print(java.lang.String fileName, char value)
print
or println
calls
that write to this file).
fileName
- The name of a filevalue
- The char
value that's to be written to the fileprintln(java.lang.String, char)
public static void println(java.lang.String fileName, double value)
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).
fileName
- The name of a filevalue
- The double
value that's to be written to the fileprint(java.lang.String, double)
public static void print(java.lang.String fileName, double value)
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).
fileName
- The name of a filevalue
- The double
value that's to be written to the fileprintln(java.lang.String, double)
public static void println(java.lang.String fileName, float value)
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).
fileName
- The name of a filevalue
- The float
value that's to be written to the fileprint(java.lang.String, float)
public static void print(java.lang.String fileName, float value)
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).
fileName
- The name of a filevalue
- The float
value that's to be written to the fileprintln(java.lang.String, float)
public static void println(java.lang.String fileName, int value)
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).
fileName
- The name of a filevalue
- The int
value that's to be written to the fileprint(java.lang.String, int)
public static void print(java.lang.String fileName, int value)
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).
fileName
- The name of a filevalue
- The int
value that's to be written to the fileprintln(java.lang.String, int)
public static void println(java.lang.String fileName, long value)
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).
fileName
- The name of a filevalue
- The long
value that's to be written to the fileprint(java.lang.String, long)
public static void print(java.lang.String fileName, long value)
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).
fileName
- The name of a filevalue
- The long
value that's to be written to the fileprintln(java.lang.String, long)
public static void println(java.lang.String fileName, java.lang.String value)
print
or println
calls
that write to this file).
fileName
- The name of a filevalue
- The String
value that's to be written to the fileprint(java.lang.String, java.lang.String)
public static void print(java.lang.String fileName, java.lang.String value)
print
or println
calls
that write to this file).
fileName
- The name of a filevalue
- The String
value that's to be written to the fileprintln(java.lang.String, java.lang.String)
public static void println(java.lang.String fileName)
print
or println
calls
that write to this file).
fileName
- The name of a file
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |