SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
TextDataWriter.java
1/*
2 * Class: TextDataWriter
3 * Description: Text data writer
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
7 * Organization: DIRO, Universite de Montreal
8 * @author David Munger
9 * @since August 2009
10 *
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 */
25package umontreal.ssj.util.io;
26
27import java.io.*;
28import java.lang.reflect.Array;
29
35public class TextDataWriter extends CachedDataWriter {
36
40
44 public final String DEFAULT_COLUMN_SEPARATOR = "\t";
45
49 public final String DEFAULT_HEADER_PREFIX = "";
50
51 protected BufferedWriter out;
52
53 protected Format format;
54 protected boolean withHeaders;
55
56 protected String columnSeparator = DEFAULT_COLUMN_SEPARATOR;
57 protected String headerPrefix = DEFAULT_HEADER_PREFIX;
58
59 protected String floatFormatString = null;
60
65 protected int getMaxFieldLength() {
66 int nRows = 0;
67 for (DataField f : super.getFields()) {
68 if (f.isArray())
69 nRows = Math.max(nRows, f.getArrayLength());
70 }
71 return nRows;
72 }
73
78 protected void outputAsColumns() throws IOException {
79
80 if (withHeaders) {
81 // output field headers
82 out.write(headerPrefix);
83 int iAnonymous = 0;
84 boolean firstColumn = true;
85 for (DataField f : super.getFields()) {
86 // separator
87 if (!firstColumn)
88 out.write(columnSeparator);
89 else
90 firstColumn = false;
91
92 if (f.getLabel() == null)
93 // anonymous field
94 out.write(String.format("_data%02d_", ++iAnonymous));
95 else
96 // named field
97 out.write(f.getLabel());
98 }
99 out.write("\n");
100 }
101
102 int nRows = getMaxFieldLength();
103
104 for (int iRow = 0; iRow < nRows; iRow++) {
105 boolean firstColumn = true;
106 for (DataField f : super.getFields()) {
107
108 // separator
109 if (!firstColumn)
110 out.write(columnSeparator);
111 else
112 firstColumn = false;
113
114 // output field data
115 if (f.isArray()) {
116 // field is an array, output its current entry
117 if (iRow < f.getArrayLength())
118 writeFormat(Array.get(f.asObject(), iRow));
119 } else {
120 // field is not an array, output only in first row
121 if (iRow == 0)
122 writeFormat(f.asObject());
123 }
124 }
125 out.write("\n");
126 }
127 }
128
133 protected void outputAsRows() throws IOException {
134
135 int iAnonymous = 0;
136
137 for (DataField f : super.getFields()) {
138
139 // output field header
140 if (withHeaders) {
141 if (f.getLabel() == null)
142 // anonymous field
143 out.write(String.format("_data%02d_", ++iAnonymous));
144 else
145 // named field
146 out.write(f.getLabel());
147
148 out.write(columnSeparator);
149 }
150
151 // output field data
152
153 if (f.isArray()) {
154
155 int nCols = f.getArrayLength();
156
157 for (int iCol = 0; iCol < nCols; iCol++) {
158
159 // separator
160 if (iCol > 0)
161 out.write(columnSeparator);
162
163 writeFormat(Array.get(f.asObject(), iCol));
164 }
165 } else {
166 writeFormat(f.asObject());
167 }
168
169 out.write("\n");
170
171 }
172 }
173
178 protected void writeFormat(Object o) throws IOException {
179 String s = null;
180 if (floatFormatString != null && (o instanceof Double || o instanceof Float))
181 s = String.format((java.util.Locale) null, floatFormatString, o); // pass null to avoid localization
182 else
183 s = o.toString();
184 out.write(s);
185 }
186
190
194 public enum Format {
195 COLUMNS, ROWS
196 }
197
206 public TextDataWriter(String filename, Format format, boolean withHeaders) throws IOException {
207 this.out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename)));
208 this.format = format;
209 this.withHeaders = withHeaders;
210 }
211
220 public TextDataWriter(File file, Format format, boolean withHeaders) throws IOException {
221 this.out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
222 this.format = format;
223 this.withHeaders = withHeaders;
224 }
225
234 public TextDataWriter(OutputStream outputStream, Format format, boolean withHeaders) throws IOException {
235 this.out = new BufferedWriter(new OutputStreamWriter(outputStream));
236 this.format = format;
237 this.withHeaders = withHeaders;
238 }
239
246 public void setFormat(Format format) {
247 this.format = format;
248 }
249
255 public void setFloatFormatString(String formatString) {
256 this.floatFormatString = formatString;
257 }
258
262 public void setColumnSeparator(String columnSeparator) {
263 this.columnSeparator = columnSeparator;
264 }
265
270 public void setHeaderPrefix(String headerPrefix) {
271 this.headerPrefix = headerPrefix;
272 }
273
277 public void close() throws IOException {
278 if (format == Format.COLUMNS)
280 else if (format == Format.ROWS)
281 outputAsRows();
282 out.close();
283 }
284
285}
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.