Package org.geotools.util
Class TableWriter
- Object
-
- Writer
-
- FilterWriter
-
- TableWriter
-
- All Implemented Interfaces:
Closeable
,Flushable
,Appendable
,AutoCloseable
public class TableWriter extends FilterWriter
A character stream that can be used to format tables. Columns are separated by tabulations ('\t'
) and rows are separated by line terminators ('\r'
,'\n'
or"\r\n"
). Every table's cells are stored in memory untilflush()
is invoked. When invoked,flush()
copy cell's contents to the underlying stream while replacing tabulations by some amount of spaces. The exact number of spaces is computed from cell's widths.TableWriter
produces correct output when displayed with a monospace font.
For example, the following code...TableWriter out = new TableWriter(new OutputStreamWriter(System.out), 3); out.write("Prénom\tNom\n"); out.nextLine('-'); out.write("Idéphonse\tLaporte\nSarah\tCoursi\nYvan\tDubois"); out.flush();
Prénom Nom --------- ------- Idéphonse Laporte Sarah Coursi Yvan Dubois
- Since:
- 2.0
- Author:
- Martin Desruisseaux (IRD)
-
-
Field Summary
Fields Modifier and Type Field Description static int
ALIGN_CENTER
A possible value for cell alignment.static int
ALIGN_LEFT
A possible value for cell alignment.static int
ALIGN_RIGHT
A possible value for cell alignment.static char
DOUBLE_HORIZONTAL_LINE
A line separator for .static String
DOUBLE_VERTICAL_LINE
A column separator for constructor.static char
SINGLE_HORIZONTAL_LINE
A line separator for .static String
SINGLE_VERTICAL_LINE
A column separator for constructor.-
Fields inherited from class FilterWriter
out
-
-
Constructor Summary
Constructors Constructor Description TableWriter(Writer out)
Creates a new table writer with a default column separator.TableWriter(Writer out, int spaces)
Creates a new table writer with the specified amount of spaces as column separator.TableWriter(Writer out, String separator)
Creates a new table writer with the specified column separator.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Flushs the table content and close the underlying stream.void
flush()
Flushs the table content to the underlying stream.int
getAlignment()
Returns the alignment for current and next cells.int
getColumnCount()
Returns the number of columns in this table.int
getRowCount()
Returns the number of rows in this table.boolean
isMultiLinesCells()
Tells if EOL characters are used for line feeds inside current cells.void
nextColumn()
Moves one column to the right.void
nextColumn(char fill)
Moves one column to the right.void
nextLine()
Moves to the first column on the next row.void
nextLine(char fill)
Moves to the first column on the next row.void
setAlignment(int alignment)
Sets the alignment for current and next cells.void
setColumnAlignment(int column, int alignment)
Sets the alignment for all cells in the specified column.void
setMultiLinesCells(boolean multiLines)
Sets the desired behavior for EOL and tabulations characters.String
toString()
Returns the table content as a string.void
write(char[] cbuf)
Writes an array of characters.void
write(char[] cbuf, int offset, int length)
Writes a portion of an array of characters.void
write(int c)
Write a single character.void
write(String string)
Writes a string.void
write(String string, int offset, int length)
Writes a portion of a string.void
writeHorizontalSeparator()
Writes an horizontal separator.-
Methods inherited from class Writer
append, append, append, nullWriter
-
-
-
-
Field Detail
-
ALIGN_LEFT
public static final int ALIGN_LEFT
A possible value for cell alignment. This specifies that the text is aligned to the left indent and extra whitespace should be placed on the right.- See Also:
- Constant Field Values
-
ALIGN_RIGHT
public static final int ALIGN_RIGHT
A possible value for cell alignment. This specifies that the text is aligned to the right indent and extra whitespace should be placed on the left.- See Also:
- Constant Field Values
-
ALIGN_CENTER
public static final int ALIGN_CENTER
A possible value for cell alignment. This specifies that the text is aligned to the center and extra whitespace should be placed equally on the left and right.- See Also:
- Constant Field Values
-
SINGLE_VERTICAL_LINE
public static final String SINGLE_VERTICAL_LINE
A column separator for constructor.- Since:
- 2.5
- See Also:
- Constant Field Values
-
DOUBLE_VERTICAL_LINE
public static final String DOUBLE_VERTICAL_LINE
A column separator for constructor.- Since:
- 2.5
- See Also:
- Constant Field Values
-
SINGLE_HORIZONTAL_LINE
public static final char SINGLE_HORIZONTAL_LINE
A line separator for .- Since:
- 2.5
- See Also:
- Constant Field Values
-
DOUBLE_HORIZONTAL_LINE
public static final char DOUBLE_HORIZONTAL_LINE
A line separator for .- Since:
- 2.5
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
TableWriter
public TableWriter(Writer out)
Creates a new table writer with a default column separator.Note: this writer may produces bad output on Windows console, unless the underlying stream use the correct codepage (e.g.
OutputStreamWriter(System.out, "Cp437")
). To display the appropriate codepage for a Windows NT console, typechcp
on the command line.- Parameters:
out
- Writer object to provide the underlying stream, ornull
if there is no underlying stream. Ifout
is null, then thetoString()
method is the only way to get the table's content.
-
TableWriter
public TableWriter(Writer out, int spaces)
Creates a new table writer with the specified amount of spaces as column separator.- Parameters:
out
- Writer object to provide the underlying stream, ornull
if there is no underlying stream. Ifout
is null, then thetoString()
method is the only way to get the table's content.spaces
- Amount of white spaces to use as column separator.
-
TableWriter
public TableWriter(Writer out, String separator)
Creates a new table writer with the specified column separator.- Parameters:
out
- Writer object to provide the underlying stream, ornull
if there is no underlying stream. Ifout
is null, then thetoString()
method is the only way to get the table's content.separator
- String to write between columns. Drawing box characters are treated specially. For example" \\u2502 "
can be used for a single-line box.- See Also:
SINGLE_VERTICAL_LINE
,DOUBLE_VERTICAL_LINE
-
-
Method Detail
-
setMultiLinesCells
public void setMultiLinesCells(boolean multiLines)
Sets the desired behavior for EOL and tabulations characters.- If
true
, EOL ('\r'
,'\n'
or"\r\n"
) and tabulations ('\t'
) characters are copied straight into the current cell, which mean that next write operations will continue inside the same cell. - If
false
, then tabulations move to next column and EOL move to the first cell of next row (i.e. tabulation and EOL are equivalent tonextColumn()
andnextLine()
calls respectively).
false
.- Parameters:
multiLines
-true
true if EOL are used for line feeds inside current cells, orfalse
if EOL move to the next row.
- If
-
isMultiLinesCells
public boolean isMultiLinesCells()
Tells if EOL characters are used for line feeds inside current cells.- Returns:
true
if EOL characters are to be write inside the cell.
-
setColumnAlignment
public void setColumnAlignment(int column, int alignment)
Sets the alignment for all cells in the specified column. This method overwrite the alignment for all previous cells in the specified column.- Parameters:
column
- The 0-based column number.alignment
- Cell alignment. Must beALIGN_LEFT
ALIGN_RIGHT
orALIGN_CENTER
.
-
setAlignment
public void setAlignment(int alignment)
Sets the alignment for current and next cells. Change to the alignment doesn't affect the alignment of previous cells and previous rows. The default alignment isALIGN_LEFT
.- Parameters:
alignment
- Cell alignment. Must beALIGN_LEFT
ALIGN_RIGHT
orALIGN_CENTER
.
-
getAlignment
public int getAlignment()
Returns the alignment for current and next cells.- Returns:
- Cell alignment:
ALIGN_LEFT
(the default),ALIGN_RIGHT
orALIGN_CENTER
.
-
getRowCount
public int getRowCount()
Returns the number of rows in this table. This count is reset to 0 byflush()
.- Returns:
- The number of rows in this table.
- Since:
- 2.5
-
getColumnCount
public int getColumnCount()
Returns the number of columns in this table.- Returns:
- The number of colunms in this table.
- Since:
- 2.5
-
write
public void write(int c)
Write a single character. IfisMultiLinesCells()
isfalse
(which is the default), then:- Tabulations (
'\t'
) are replaced bynextColumn()
invocations. - Line separators (
'\r'
,'\n'
or"\r\n"
) are replaced bynextLine()
invocations.
- Overrides:
write
in classFilterWriter
- Parameters:
c
- Character to write.
- Tabulations (
-
write
public void write(String string)
Writes a string. Tabulations and line separators are interpreted as bywrite(int)
.
-
write
public void write(String string, int offset, int length)
Writes a portion of a string. Tabulations and line separators are interpreted as bywrite(int)
.- Overrides:
write
in classFilterWriter
- Parameters:
string
- String to write.offset
- Offset from which to start writing characters.length
- Number of characters to write.
-
write
public void write(char[] cbuf)
Writes an array of characters. Tabulations and line separators are interpreted as bywrite(int)
.
-
write
public void write(char[] cbuf, int offset, int length)
Writes a portion of an array of characters. Tabulations and line separators are interpreted as bywrite(int)
.- Overrides:
write
in classFilterWriter
- Parameters:
cbuf
- Array of characters.offset
- Offset from which to start writing characters.length
- Number of characters to write.
-
writeHorizontalSeparator
public void writeHorizontalSeparator()
Writes an horizontal separator.
-
nextColumn
public void nextColumn()
Moves one column to the right. Next write operations will occur in a new cell on the same row.
-
nextColumn
public void nextColumn(char fill)
Moves one column to the right. Next write operations will occur in a new cell on the same row. This method fill every remaining space in the current cell with the specified character. For example callingnextColumn('*')
from the first character of a cell is a convenient way to put a pad value in this cell.- Parameters:
fill
- Character filling the cell (default to whitespace).
-
nextLine
public void nextLine()
Moves to the first column on the next row. Next write operations will occur on a new row.
-
nextLine
public void nextLine(char fill)
Moves to the first column on the next row. Next write operations will occur on a new row. This method fill every remaining cell in the current row with the specified character. CallingnextLine('-')
from the first column of a row is a convenient way to fill this row with a line separator.- Parameters:
fill
- Character filling the rest of the line (default to whitespace). This caracter may be use as a row separator.- See Also:
SINGLE_HORIZONTAL_LINE
,DOUBLE_HORIZONTAL_LINE
-
flush
public void flush() throws IOException
Flushs the table content to the underlying stream. This method should not be called before the table is completed (otherwise, columns may have the wrong width).- Specified by:
flush
in interfaceFlushable
- Overrides:
flush
in classFilterWriter
- Throws:
IOException
- if an output operation failed.
-
close
public void close() throws IOException
Flushs the table content and close the underlying stream.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classFilterWriter
- Throws:
IOException
- if an output operation failed.
-
-