public class TextOutput extends Object implements Flushable
PrintStream
The indentation level starts at 0 and can be changed using
indentMore()
and indentLess()
.
Example of usage:
TextOutput to = new TextOutput(System.out);
to.println("Hello!");
to.indentMore("My favorite fruits:");
to.println("Banana");
to.println("Apple");
to.indentLess();
to.flush();
Constructor and Description |
---|
TextOutput()
Creates a
TextOutput that discards all the messages |
TextOutput(PrintStream ps)
Creates a
TextOutput bound to a PrintStream |
Modifier and Type | Method and Description |
---|---|
void |
flush()
Flushes the underlying
PrintStream |
void |
indentLess()
Decrements the indentation level
|
void |
indentMore()
Increments the indentation level
|
void |
indentMore(String message)
Prints a message and increments the indentation level
|
void |
println(String x)
Prints a message on a separate line
|
public TextOutput(PrintStream ps)
TextOutput
bound to a PrintStream
ps
- the PrintStream
to print messages topublic TextOutput()
TextOutput
that discards all the messagespublic void println(String x)
The message is indented by the current indentation level (starting at 0)
and printed using the specified PrintStream
.
Mimics PrintStream.println(String x)
.
x
- message to printpublic void indentMore()
If the code between indentMore()
and indentLess()
may throw
a (checked) exception, surround the code with a
try
-finally
block like this to maintain consistency:
TextOutput to;
to.indentMore();
try {
// Do stuff, possibly throwing an exception
// Possibly catch the exception
} finally {
to.indentLess();
}
public void indentMore(String message)
message
- message to printpublic void indentLess()
public void flush() throws IOException
PrintStream
flush
in interface Flushable
IOException
- if the underlying PrintStream.flush()
throws an
IOException
Copyright © 2015–2016 Hobrasoft s.r.o.. All rights reserved.