MATSIM
SimpleWriter.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2011 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** */
19 
20 package org.matsim.pt.counts;
21 
22 import java.io.BufferedWriter;
23 import java.io.Closeable;
24 import java.io.Flushable;
25 import java.io.IOException;
26 
27 import org.matsim.core.utils.io.IOUtils;
29 
37 public class SimpleWriter implements Closeable, Flushable {
38 
39  private final BufferedWriter writer;
40 
41  public SimpleWriter(final String outputFilename) {
42  writer = IOUtils.getBufferedWriter(outputFilename);
43  }
44 
45  public void write(char[] c) {
46  if (writer != null)
47  try {
48  writer.write(c);
49  } catch (IOException e) {
50  throw new UncheckedIOException(e);
51  }
52  }
53 
54  public void write(char c) {
55  if (writer != null)
56  try {
57  writer.write(c);
58  } catch (IOException e) {
59  throw new UncheckedIOException(e);
60  }
61  }
62 
63  public void write(String s) {
64  if (writer != null) {
65  try {
66  writer.write(s);
67  } catch (IOException e) {
68  System.err.println("writer was not initialized yet!");
69  throw new UncheckedIOException(e);
70  }
71  }
72  }
73 
74  public void write(Object o) {
75  write(o.toString());
76  }
77 
78  public void writeln(String s) {
79  write(s + "\n");
80  }
81 
82  public void writeln(Object o) {
83  write(o + "\n");
84  }
85 
86  public void writeln() {
87  write('\n');
88  }
89 
90  @Override
91  public void close() {
92  try {
93  writer.close();
94  } catch (IOException e) {
95  throw new UncheckedIOException(e);
96  }
97  }
98 
99  @Override
100  public void flush() {
101  try {
102  writer.flush();
103  } catch (IOException e) {
104  throw new UncheckedIOException(e);
105  }
106  }
107 
108  public void writeln(StringBuffer line) {
109  writeln(line.toString());
110  }
111 
112 }
SimpleWriter(final String outputFilename)
static BufferedWriter getBufferedWriter(URL url, Charset charset, boolean append)
Definition: IOUtils.java:387
void writeln(StringBuffer line)