25package umontreal.ssj.util.io;
28import java.lang.reflect.Array;
51 protected BufferedWriter out;
54 protected boolean withHeaders;
59 protected String floatFormatString =
null;
69 nRows = Math.max(nRows, f.getArrayLength());
82 out.write(headerPrefix);
84 boolean firstColumn =
true;
88 out.write(columnSeparator);
92 if (f.getLabel() ==
null)
94 out.write(String.format(
"_data%02d_", ++iAnonymous));
97 out.write(f.getLabel());
104 for (
int iRow = 0; iRow < nRows; iRow++) {
105 boolean firstColumn =
true;
110 out.write(columnSeparator);
117 if (iRow < f.getArrayLength())
141 if (f.getLabel() ==
null)
143 out.write(String.format(
"_data%02d_", ++iAnonymous));
146 out.write(f.getLabel());
148 out.write(columnSeparator);
155 int nCols = f.getArrayLength();
157 for (
int iCol = 0; iCol < nCols; iCol++) {
161 out.write(columnSeparator);
180 if (floatFormatString !=
null && (o instanceof Double || o instanceof Float))
181 s = String.format((java.util.Locale)
null, floatFormatString, o);
207 this.out =
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(filename)));
208 this.format = format;
209 this.withHeaders = withHeaders;
221 this.out =
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(file)));
222 this.format = format;
223 this.withHeaders = withHeaders;
235 this.out =
new BufferedWriter(
new OutputStreamWriter(outputStream));
236 this.format = format;
237 this.withHeaders = withHeaders;
247 this.format = format;
256 this.floatFormatString = formatString;
263 this.columnSeparator = columnSeparator;
271 this.headerPrefix = headerPrefix;
277 public void close() throws IOException {
278 if (format ==
Format.COLUMNS)
280 else if (format ==
Format.ROWS)
CachedDataWriter()
Class constructor.
This class represents a data field from a file read by an instance of a class implementing DataReader...
void setFormat(Format format)
Changes the output format.
final String DEFAULT_HEADER_PREFIX
Default value for the header prefix.
int getMaxFieldLength()
Returns the maximum field length.
void setHeaderPrefix(String headerPrefix)
Changes the header prefix (a string that indicates the beginning of the header line for the COLUMNS f...
void close()
Flushes any pending data and closes the file or stream.
void writeFormat(Object o)
Formats the object in accordance with the current format strings settings.
TextDataWriter(String filename, Format format, boolean withHeaders)
Class constructor.
void outputAsColumns()
Outputs fields as columns.
void setFloatFormatString(String formatString)
Sets the format string used to output floating point numbers.
final String DEFAULT_COLUMN_SEPARATOR
Default value for the column separator.
void setColumnSeparator(String columnSeparator)
Changes the column separator.
TextDataWriter(OutputStream outputStream, Format format, boolean withHeaders)
Class constructor.
TextDataWriter(File file, Format format, boolean withHeaders)
Class constructor.
void outputAsRows()
Outputs fields as rows.
Output format: organize fields as columns or as rows.