public class JustifyTextWriter
extends java.io.PrintWriter
The JustifyTextWriter class is a filter class. A JustifyTextWriter object wraps a Writer or OutputStream object, filtering output to the wrapped object so that output lines are right-justified, left-justified or centered within a field. (Strictly speaking, there's no need for this class to support left-justifying lines, since that's the default. It's supported here solely for completeness.)
For instance, given the following input:
This is the first line. This, a longer line, is the second.
A JustifyTextWriter that's right-justifying lines in a 50-character field would produce:
* This is the first line.| <-- This, a longer line, is the second.| <--
(The arrows and vertical bars would obviously not be output. They're there to show where the field ends.)
A JustifyTextWriter that's centering lines in a 50-character field would produce:
This is the first line. | <-- This, a longer line, is the second. | <--
For an interesting effect, consider wrapping a JustifyTextWriter
inside a WordWrapWriter
to get word-wrapped, justified output. For
instance, to wrap words on a 60-character boundary, and then center them
in the same field, use code like this:
final int WIDTH = 60; WordWrapWriter out = new WordWrapWriter (new JustifyWriter (JustifyWriter.CENTER, WIDTH), WIDTH);
Notes
WordWrapWriter
,
TextUtil.rightJustifyString(String,int)
,
TextUtil.leftJustifyString(String,int)
,
TextUtil.centerString(String,int)
,
Writer
,
PrintWriter
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_LINE_LENGTH
The default line length.
|
Constructor and Description |
---|
JustifyTextWriter(java.io.OutputStream output,
JustifyStyle justification)
Build an JustifyTextWriter object that will write its
output to the specified OutputStream object, using the
default line length of 79.
|
JustifyTextWriter(java.io.OutputStream output,
JustifyStyle justification,
int lineLength)
Build an JustifyTextWriter object that will write its
output to the specified OutputStream object, using the
specified line length.
|
JustifyTextWriter(java.io.PrintWriter output,
JustifyStyle justification)
Build an JustifyTextWriter object that will write its
output to the specified PrintWriter object, using the default
line length of 79.
|
JustifyTextWriter(java.io.PrintWriter output,
JustifyStyle justification,
int lineLength)
Build an JustifyTextWriter object that will write its
output to the specified PrintWriter object, using the
specified line length.
|
JustifyTextWriter(java.io.Writer output,
JustifyStyle justification)
Build an JustifyTextWriter object that will write its
output to the specified Writer object, using the default
line length of 79.
|
JustifyTextWriter(java.io.Writer output,
JustifyStyle justification,
int lineLength)
Build an JustifyTextWriter object that will write its
output to the specified Writer object, using the specified
line length.
|
Modifier and Type | Method and Description |
---|---|
boolean |
checkError()
Flush the stream and check its error state.
|
void |
close()
Close the stream, flushing it first.
|
void |
flush()
Flush the stream.
|
JustifyStyle |
getJustification()
Retrieve the current justification style.
|
int |
getLineLength()
Retrieve the current line length.
|
void |
print(boolean b)
Print a boolean.
|
void |
print(char c)
Print a character.
|
void |
print(char[] s)
Print an array of characters.
|
void |
print(double d)
Print a double.
|
void |
print(float f)
Print a float.
|
void |
print(int i)
Print an integer.
|
void |
print(long l)
Print a long.
|
void |
print(java.lang.Object x)
Print an Object.
|
void |
print(short s)
Print a short.
|
void |
print(java.lang.String s)
Print a String.
|
void |
println()
End the current line.
|
void |
println(boolean b)
Print a boolean and finish the line.
|
void |
println(char c)
Print a character and finish the line.
|
void |
println(char[] s)
Print an array of characters.
|
void |
println(double d)
Print a double and finish the line.
|
void |
println(float f)
Print a float and finish the line.
|
void |
println(int i)
Print an integer.
|
void |
println(long l)
Print a long and finish the line.
|
void |
println(java.lang.Object x)
Print an Object and finish the line.
|
void |
println(short s)
Print a short and finish the line.
|
void |
println(java.lang.String s)
Print a String and finish the line.
|
void |
setJustification(JustifyStyle style)
Set or change the current justification style.
|
void |
setLineLength(int newLineLength)
Set the line length.
|
void |
write(char[] cbuf)
Write an array of characters.
|
void |
write(char[] cbuf,
int off,
int len)
Write a portion of an array of characters to the underlying
output object.
|
void |
write(int c)
Write a single character.
|
void |
write(java.lang.String s)
Write a string.
|
void |
write(java.lang.String s,
int off,
int len)
Write a portion of a String of characters to the underlying
output object.
|
public static final int DEFAULT_LINE_LENGTH
public JustifyTextWriter(java.io.Writer output, JustifyStyle justification)
output
- Where the output goes.justification
- How to justify the outputDEFAULT_LINE_LENGTH
,
JustifyTextWriter(Writer,JustifyStyle,int)
,
JustifyTextWriter(PrintWriter,JustifyStyle)
,
JustifyTextWriter(OutputStream,JustifyStyle)
,
JustifyStyle
,
Writer
public JustifyTextWriter(java.io.PrintWriter output, JustifyStyle justification)
output
- Where the output goes.justification
- How to justify the outputDEFAULT_LINE_LENGTH
,
JustifyTextWriter(Writer,JustifyStyle)
,
JustifyTextWriter(PrintWriter,JustifyStyle,int)
,
JustifyTextWriter(OutputStream,JustifyStyle)
,
JustifyStyle
,
Writer
public JustifyTextWriter(java.io.OutputStream output, JustifyStyle justification)
output
- Where the output goes.justification
- How to justify the outputDEFAULT_LINE_LENGTH
,
JustifyTextWriter(OutputStream,JustifyStyle,int)
,
JustifyTextWriter(Writer,JustifyStyle)
,
JustifyTextWriter(PrintWriter,JustifyStyle)
,
JustifyStyle
,
OutputStream
public JustifyTextWriter(java.io.Writer output, JustifyStyle justification, int lineLength)
output
- Where the output goes.justification
- How to justify the outputlineLength
- The desired line length.DEFAULT_LINE_LENGTH
,
JustifyTextWriter(Writer,JustifyStyle)
,
JustifyTextWriter(PrintWriter,JustifyStyle,int)
,
JustifyTextWriter(OutputStream,JustifyStyle,int)
,
JustifyStyle
,
Writer
public JustifyTextWriter(java.io.PrintWriter output, JustifyStyle justification, int lineLength)
output
- Where the output goes.justification
- How to justify the outputlineLength
- The desired line length.DEFAULT_LINE_LENGTH
,
JustifyTextWriter(PrintWriter,JustifyStyle)
,
JustifyTextWriter(Writer,JustifyStyle,int)
,
JustifyTextWriter(OutputStream,JustifyStyle,int)
,
JustifyStyle
,
Writer
public JustifyTextWriter(java.io.OutputStream output, JustifyStyle justification, int lineLength)
output
- Where the output goes.justification
- How to justify the outputlineLength
- The desired line length.DEFAULT_LINE_LENGTH
,
JustifyTextWriter(OutputStream,JustifyStyle)
,
JustifyTextWriter(Writer,JustifyStyle,int)
,
JustifyTextWriter(PrintWriter,JustifyStyle,int)
,
JustifyStyle
,
Writer
public boolean checkError()
checkError
in class java.io.PrintWriter
public void close()
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.PrintWriter
public void flush()
flush
in interface java.io.Flushable
flush
in class java.io.PrintWriter
public JustifyStyle getJustification()
setJustification(org.clapper.util.io.JustifyStyle)
public void setJustification(JustifyStyle style)
style
- The new justification stylesetJustification(org.clapper.util.io.JustifyStyle)
public int getLineLength()
public void setLineLength(int newLineLength) throws java.lang.IndexOutOfBoundsException
newLineLength
- The new line length to use. A value of 0
disables wrapping.java.lang.IndexOutOfBoundsException
- the value is negativepublic void print(boolean b)
print
in class java.io.PrintWriter
b
- The boolean to printpublic void print(char c)
print
in class java.io.PrintWriter
c
- The character to printpublic void print(char[] s)
print
in class java.io.PrintWriter
s
- The array of characters to printpublic void print(double d)
print
in class java.io.PrintWriter
d
- The double floating point number to printpublic void print(float f)
print
in class java.io.PrintWriter
f
- The floating point number to printpublic void print(int i)
print
in class java.io.PrintWriter
i
- The integer to printpublic void print(long l)
print
in class java.io.PrintWriter
l
- The long to printpublic void print(short s)
s
- The short to printpublic void print(java.lang.String s)
print
in class java.io.PrintWriter
s
- The String to print.public void print(java.lang.Object x)
print
in class java.io.PrintWriter
x
- The object to print.public void println()
println
in class java.io.PrintWriter
public void println(boolean b)
println
in class java.io.PrintWriter
b
- The boolean to printpublic void println(char c)
println
in class java.io.PrintWriter
c
- The character to printpublic void println(char[] s)
println
in class java.io.PrintWriter
s
- The array of characters to printpublic void println(double d)
println
in class java.io.PrintWriter
d
- The double floating point number to printpublic void println(float f)
println
in class java.io.PrintWriter
f
- The floating point number to printpublic void println(int i)
println
in class java.io.PrintWriter
i
- The integer to printpublic void println(long l)
println
in class java.io.PrintWriter
l
- The long to printpublic void println(short s)
s
- The short to printpublic void println(java.lang.String s)
println
in class java.io.PrintWriter
s
- The String to print.public void println(java.lang.Object x)
println
in class java.io.PrintWriter
x
- The object to print.public void write(int c)
write
in class java.io.PrintWriter
c
- Character to writepublic void write(char[] cbuf, int off, int len)
write
in class java.io.PrintWriter
cbuf
- Array of charactersoff
- Offset from which to start writing characterslen
- Number of characters to writepublic void write(java.lang.String s, int off, int len)
write
in class java.io.PrintWriter
s
- String from which to writeoff
- Offset from which to start writing characterslen
- Number of characters to writepublic void write(java.lang.String s)
write
in class java.io.PrintWriter
s
- String to writepublic void write(char[] cbuf)
write
in class java.io.PrintWriter
cbuf
- Array of characters to write