Repository navigation
Expand file tree
/
Copy pathOutputWriter.java
More file actions
318 lines (282 loc) · 10.3 KB
/
Copy pathOutputWriter.java
File metadata and controls
318 lines (282 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import java.io.BufferedWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.*;
public class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
/**
* Writes the end of line. This is a no - op for text files as it does not have a newline
*/
public void endl(){
writer.write("--------------------------------");
}
/**
* Prints a table to the console. This method is for debugging purposes only. If you want to be notified of the progress of the print use #printLine ( String )
*
* @param table - char [] to be
*/
public void printTable(char[][] table) {
for (char[] row : table) {
printLine(new String(row));
}
}
/**
* Prints a table of integers. The table is assumed to be an array of integers where each integer is represented by a pair of integers in the form [ i ] [ j ]
*
* @param table - the table to be
*/
public void printTable(int[][] table) {
// Prints the table in the table.
for(int i = 0;i < table.length;i++){
// Prints the table to the console.
for(int j = 0; j< table.length;j++){
writer.print(table[i][j]);
writer.print(' ');
}
writer.println();
}
}
/**
* Prints a table of longs. The table is assumed to be of the form { x1 x2... xn } where x1 and x2 are the indices of the rows and columns in the table.
*
* @param table - the table to be printed. The values are assumed to be non - negative
*/
public void printTable(long[][] table) {
// Prints the table in the table.
for(int i = 0;i < table.length;i++){
// Prints the table to the console.
for(int j = 0; j< table.length;j++){
writer.print(table[i][j]);
writer.print(' ');
}
writer.println();
}
}
/**
* Prints a 2D double array. The values are printed in column - major order. For example the table [ 0 ] [ 1 ] would be printed as follows
*
* @param table - the 2D double array to
*/
public void printTable(double[][] table) {
// Prints the table in the table.
for(int i = 0;i < table.length;i++){
// Prints the table to the console.
for(int j = 0; j< table.length;j++){
writer.print(table[i][j]);
writer.print(' ');
}
writer.println();
}
}
/**
* Print a character array. This method prints a line termination sequence after printing the character array to the underlying stream.
*
* @param array - The character array to be printed. null values are not printed
*/
public void print(char[] array) {
writer.print(array);
}
/**
* Prints an array of objects separated by spaces. Each object is printed using the #writer () method for its printing
*/
public void print(Object... objects) {
// Prints the objects in the list.
for (int i = 0; i < objects.length; i++) {
// Print the current value of the field.
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
/**
* Prints an array of integers to the file with a comma between each element. The array is treated as a single integer and not converted to a string.
*
* @param array - the array to be printed to the file ; may not be
*/
public void print(int[] array) {
// Prints the array of values.
for (int i = 0; i < array.length; i++) {
// Print the current value of the field.
if (i != 0) {
writer.print(' ');
}
writer.print(array[i]);
}
}
/**
* Prints an array of doubles to the file with a comma between each value. The array is treated as a double array and each value is printed independently.
*
* @param array - the array to be printed to the file ; the array itself cannot be
*/
public void print(double[] array) {
// Prints the array of values.
for (int i = 0; i < array.length; i++) {
// Print the current value of the field.
if (i != 0) {
writer.print(' ');
}
writer.print(array[i]);
}
}
/**
* Prints an long array. The array is treated as a space - separated list of longs and each long is printed with a space between the elements.
*
* @param array - the long array to be printed. null values are ignored
*/
public void print(long[] array) {
// Prints the array of values.
for (int i = 0; i < array.length; i++) {
// Print the current value of the field.
if (i != 0) {
writer.print(' ');
}
writer.print(array[i]);
}
}
/**
* This method prints an array of int to the file with a new line. This method behaves as though it invokes #print ( int ) and then #println ().
*
* @param array - The array to be printed to the file. This array is not modified
*/
public void printLine(int[] array) {
print(array);
writer.println();
}
/**
* This method prints a double array on the file followed by a line terminator. This method behaves as though it invokes #print ( double ) and then #println ().
*
* @param array - The double array to be printed on the file
*/
public void printLine(double[] array) {
print(array);
writer.println();
}
/**
* This method prints a long array on the stream. The value printed is determined by the #print ( long ) method.
*
* @param array - The long array to be printed on the stream
*/
public void printLine(long[] array) {
print(array);
writer.println();
}
/**
* Print a line of text to the output stream. This method behaves as though it invokes #writeLine
*/
public void printLine() {
writer.println();
}
/**
* Print a list of objects and terminate the line with a new line character. This method behaves as though it invokes #print ( Object [] ) and then #println ()
*/
public void printLine(Object... objects) {
print(objects);
writer.println();
}
/**
* Print a character. This method behaves as though it invokes the writer's print method. The printing is done using the current encoding.
*
* @param i - The character to be printed. If i is a null character the method #print ( char ) is called
*/
public void print(char i) {
writer.print(i);
}
/**
* Prints a character to the file followed by a line terminator. This method behaves as though it invokes the writer's println method.
*
* @param i - The character to be printed to the file followed by a line terminator
*/
public void printLine(char i) {
writer.println(i);
}
/**
* Prints a character array on the file followed by a line terminator. This method behaves as though it invokes #write ( char [] ) and then #println ().
*
* @param array - the array to be printed on the file ; characters are converted to
*/
public void printLine(char[] array) {
writer.println(array);
}
/**
* Prints a formatted string to the writer. This method behaves as though it invokes #write ( Writer ).
*
* @param format - The format string to print. The format string is interpreted as follows : Each element of the array is converted to a String using String#format ( String
*/
public void printFormat(String format, Object... objects) {
writer.printf(format, objects);
}
/**
* Closes the writer. After this method is called no more data can be written to the writer. Note that you cannot call this method more than once
*/
public void close() {
writer.close();
}
/**
* Flushes the output. This is a no - op if there is no output to be written to
*/
public void flush() {
writer.flush();
}
/**
* This method prints a long. This is a convenience method that simply calls writer. print ( i ).
*
* @param i - The long to print. This value is converted to a string using the String. valueOf () method
*/
public void print(long i) {
writer.print(i);
}
/**
* This method prints a long to the file with a line terminator. This method behaves as though it invokes the writer's println method.
*
* @param i - The long to be printed to the file with a line terminator
*/
public void printLine(long i) {
writer.println(i);
}
/**
* This method prints an integer to the stream. The value printed is determined by the write method of the DataOutput class.
*
* @param i - The integer to be printed to the stream. The value can be converted to a string using the String. valueOf
*/
public void print(int i) {
// System.out.println("isvalled");
writer.print(i);
}
/**
* Print an integer and then terminate the line with a new line secquence. This method behaves as though it invokes #writer. println ().
*
* @param i - The integer to be printed. It is assumed that 0 < = i < Integer. MAX_VALUE
*/
public void printLine(int i) {
writer.println(i);
}
/**
* Prints an array of integers on the same line. This method is equivalent to printLine ( array )
*
* @param array - The array of integers to
*/
public void separateLines(int[] array) {
for (int i : array) {
printLine(i);
}
}
/**
* Prints a list of objects. Each object is printed with a new line after it. This method is useful for dumping a list of objects to the console.
*
* @param answer - the list to be printed. It must be a List
*/
public void printList(List<?> answer) {
for (Object o : answer) {
printLine(o);
}
}
}