public class EasyWriter
extends java.lang.Object
Appendix to:
Java Methods: Object-Oriented Programming and Data Structures, 3rd AP Edition
(Skylight Publishing 2015, ISBN 978-0-9824775-6-4)
EasyWriter provides simple methods for opening and writing to text files. All exceptions are handled inside the class and are hidden from the user.
Example: ======= EasyWriter outputFile = new EasyWriter("anyname.txt"); if (outputFile.bad()) { System.err.println("*** Cannot create anyname.txt ***"); System.exit(1); } outputFile.print("2 + 2 = "); outputFile.println(4); outputFile.printf("2 + 2 = %d\n", 4); outputFile.println(); // an extra blank line outputFile.close(); outputFile = new EasyWriter("anyname.txt", "app"); if (outputFile.bad()) { System.err.println("*** Cannot create anyname.txt ***"); System.exit(1); } outputFile.printf("pi = %5.2f\n", Math.pi); outputFile.close();
Constructor and Description |
---|
EasyWriter(java.lang.String fileName)
Constructs an
EasyWriter associated with a new file
(or truncates an existing file). |
EasyWriter(java.lang.String fileName,
java.lang.String mode)
Constructs an
EasyWriter that can append data to an
existing file. |
Modifier and Type | Method and Description |
---|---|
boolean |
bad()
Checks the status of the file.
|
void |
close()
Closes the file.
|
void |
print(char ch)
Writes one character to the file.
|
void |
print(double x)
Writes a double to the file.
|
void |
print(int k)
Writes an integer to the file.
|
void |
print(java.lang.Object obj)
Writes an object to the file.
|
void |
print(java.lang.String s)
Writes a string to the file.
|
void |
printf(java.lang.String format,
java.lang.Object... args)
A convenience method to write a formatted string to this
EasyWriter using the specified format string and parameters. |
void |
println()
Writes a newline character to the file.
|
void |
println(char ch)
Writes one character and newline to the file.
|
void |
println(double x)
Writes a double and newline to the file.
|
void |
println(int k)
Writes an integer and newline to the file.
|
void |
println(java.lang.Object obj)
Writes an object and newline to the file.
|
void |
println(java.lang.String s)
Writes a string and newline to the file.
|
public EasyWriter(java.lang.String fileName)
EasyWriter
associated with a new file
(or truncates an existing file).fileName
- the name of the file to be created.public EasyWriter(java.lang.String fileName, java.lang.String mode)
EasyWriter
that can append data to an
existing file.fileName
- the name of the file to be created.mode
- if mode
is "app" and the file exists,
then opens the file in append mode.public void close()
public boolean bad()
public void print(char ch)
ch
- character to be written.public void print(int k)
k
- number to be written.public void print(double x)
x
- number to be written.public void print(java.lang.String s)
s
- string to be written.public void print(java.lang.Object obj)
obj
- object to be written.public void println()
public void println(char ch)
ch
- character to be written.public void println(int k)
k
- number to be written.public void println(double x)
x
- number to be written.public void println(java.lang.String s)
s
- string to be written.public void println(java.lang.Object obj)
obj
- object to be written.public void printf(java.lang.String format, java.lang.Object... args)
EasyWriter
using the specified format string and parameters.