libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / java / io / PrintWriter.java
blob5b4294cbaa74aa3481f19e33b5118c82191a6202
1 /* PrintWriter.java -- prints primitive values and objects to a stream as text
2 Copyright (C) 1998, 1999, 2000, 2001, 2005 Free Software Foundation
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package java.io;
40 import java.util.Locale;
41 import java.util.Formatter;
43 /* Written using "Java Class Libraries", 2nd edition, plus online
44 * API docs for JDK 1.2 beta from http://www.javasoft.com.
45 * Status: Believed complete and correct.
46 * However, should use native methods for conversion.
49 /**
50 * This class prints Java primitive values and objects to a stream as
51 * text. None of the methods in this class throw an exception. However,
52 * errors can be detected by calling the <code>checkError()</code> method.
53 * Additionally, this stream can be designated as "autoflush" when
54 * created so that any writes are automatically flushed to the underlying
55 * output sink whenever one of the <code>println</code> methods is
56 * called. (Note that this differs from the <code>PrintStream</code>
57 * class which also auto-flushes when it encounters a newline character
58 * in the chars written).
60 * @author Per Bothner (bothner@cygnus.com)
61 * @author Aaron M. Renn (arenn@urbanophile.com)
62 * @date April 17, 1998.
64 public class PrintWriter extends Writer
66 /**
67 * <code>true</code> if auto-flush is enabled, <code>false</code> otherwise
69 private boolean autoflush;
71 /**
72 * This boolean indicates whether or not an error has ever occurred
73 * on this stream.
75 private boolean error;
77 /**
78 * Indicates whether or not the stream has been closed.
80 private boolean closed;
82 /**
83 * This is the underlying <code>Writer</code> we are sending output
84 * to
86 protected Writer out;
88 /**
89 * This method intializes a new <code>PrintWriter</code> object to write
90 * to the specified output sink. The form of the constructor does not
91 * enable auto-flush functionality.
93 * @param wr The <code>Writer</code> to write to.
95 public PrintWriter(Writer wr)
97 super(wr.lock);
98 this.out = wr;
102 * This method intializes a new <code>PrintWriter</code> object to write
103 * to the specified output sink. This constructor also allows "auto-flush"
104 * functionality to be specified where the stream will be flushed after
105 * every line is terminated or newline character is written.
107 * @param wr The <code>Writer</code> to write to.
108 * @param autoflush <code>true</code> to flush the stream after every
109 * line, <code>false</code> otherwise
111 public PrintWriter(Writer wr, boolean autoflush)
113 super(wr.lock);
114 this.out = wr;
115 this.autoflush = autoflush;
119 * This method initializes a new <code>PrintWriter</code> object to write
120 * to the specified <code>OutputStream</code>. Characters will be converted
121 * to chars using the system default encoding. Auto-flush functionality
122 * will not be enabled.
124 * @param out The <code>OutputStream</code> to write to
126 public PrintWriter(OutputStream out)
128 super();
129 this.out = new OutputStreamWriter(out);
130 this.lock = this.out;
134 * This method initializes a new <code>PrintWriter</code> object to write
135 * to the specified <code>OutputStream</code>. Characters will be converted
136 * to chars using the system default encoding. This form of the
137 * constructor allows auto-flush functionality to be enabled if desired
139 * @param out The <code>OutputStream</code> to write to
140 * @param autoflush <code>true</code> to flush the stream after every
141 * <code>println</code> call, <code>false</code> otherwise.
143 public PrintWriter(OutputStream out, boolean autoflush)
145 this(out);
146 this.autoflush = autoflush;
150 * This initializes a new PrintWriter object to write to the specified
151 * file. It creates a FileOutputStream object and wraps it in an
152 * OutputStreamWriter using the default encoding.
153 * @param file name of the file to write to
154 * @throws FileNotFoundException if the file cannot be written or created
156 * @since 1.5
158 public PrintWriter(String file) throws FileNotFoundException
160 this(new FileOutputStream(file));
164 * This initializes a new PrintWriter object to write to the specified
165 * file. It creates a FileOutputStream object and wraps it in an
166 * OutputStreamWriter using the specified encoding.
167 * @param file name of the file to write to
168 * @param enc the encoding to use
169 * @throws FileNotFoundException if the file cannot be written or created
170 * @throws UnsupportedEncodingException if the encoding is not supported
172 * @since 1.5
174 public PrintWriter(String file, String enc)
175 throws FileNotFoundException, UnsupportedEncodingException
177 this(new OutputStreamWriter(new FileOutputStream(file), enc));
181 * This initializes a new PrintWriter object to write to the specified
182 * file. It creates a FileOutputStream object and wraps it in an
183 * OutputStreamWriter using the default encoding.
184 * @param file the file to write to
185 * @throws FileNotFoundException if the file cannot be written or created
187 * @since 1.5
189 public PrintWriter(File file) throws FileNotFoundException
191 this(new FileOutputStream(file));
195 * This initializes a new PrintWriter object to write to the specified
196 * file. It creates a FileOutputStream object and wraps it in an
197 * OutputStreamWriter using the specified encoding.
198 * @param file the file to write to
199 * @param enc the encoding to use
200 * @throws FileNotFoundException if the file cannot be written or created
201 * @throws UnsupportedEncodingException if the encoding is not supported
203 * @since 1.5
205 public PrintWriter(File file, String enc)
206 throws FileNotFoundException, UnsupportedEncodingException
208 this(new OutputStreamWriter(new FileOutputStream(file), enc));
212 * This method can be called by subclasses to indicate that an error
213 * has occurred and should be reported by <code>checkError</code>.
215 protected void setError()
217 error = true;
221 * This method checks to see if an error has occurred on this stream. Note
222 * that once an error has occurred, this method will continue to report
223 * <code>true</code> forever for this stream. Before checking for an
224 * error condition, this method flushes the stream.
226 * @return <code>true</code> if an error has occurred,
227 * <code>false</code> otherwise
229 public boolean checkError()
231 if (! closed)
232 flush();
233 return error;
237 * This method flushes any buffered chars to the underlying stream and
238 * then flushes that stream as well.
240 public void flush()
244 out.flush();
246 catch (IOException ex)
248 error = true;
253 * This method closes this stream and all underlying streams.
255 public void close()
259 out.close();
260 closed = true;
262 catch (IOException ex)
264 error = true;
269 * This method prints a <code>String</code> to the stream. The actual
270 * value printed depends on the system default encoding.
272 * @param str The <code>String</code> to print.
274 public void print(String str)
276 write(str == null ? "null" : str);
280 * This method prints a char to the stream. The actual value printed is
281 * determined by the character encoding in use.
283 * @param ch The <code>char</code> value to be printed
285 public void print(char ch)
287 write((int) ch);
291 * This method prints an array of characters to the stream. The actual
292 * value printed depends on the system default encoding.
294 * @param charArray The array of characters to print.
296 public void print(char[] charArray)
298 write(charArray, 0, charArray.length);
302 * This methods prints a boolean value to the stream. <code>true</code>
303 * values are printed as "true" and <code>false</code> values are printed
304 * as "false".
306 * @param bool The <code>boolean</code> value to print
308 public void print(boolean bool)
310 // We purposely call write() and not print() here. This preserves
311 // compatibility with JDK 1.2.
312 write (bool ? "true" : "false");
316 * This method prints an integer to the stream. The value printed is
317 * determined using the <code>String.valueOf()</code> method.
319 * @param inum The <code>int</code> value to be printed
321 public void print(int inum)
323 // We purposely call write() and not print() here. This preserves
324 // compatibility with JDK 1.2.
325 write(Integer.toString(inum));
329 * This method prints a long to the stream. The value printed is
330 * determined using the <code>String.valueOf()</code> method.
332 * @param lnum The <code>long</code> value to be printed
334 public void print(long lnum)
336 // We purposely call write() and not print() here. This preserves
337 // compatibility with JDK 1.2.
338 write(Long.toString(lnum));
342 * This method prints a float to the stream. The value printed is
343 * determined using the <code>String.valueOf()</code> method.
345 * @param fnum The <code>float</code> value to be printed
347 public void print(float fnum)
349 // We purposely call write() and not print() here. This preserves
350 // compatibility with JDK 1.2.
351 write(Float.toString(fnum));
355 * This method prints a double to the stream. The value printed is
356 * determined using the <code>String.valueOf()</code> method.
358 * @param dnum The <code>double</code> value to be printed
360 public void print(double dnum)
362 // We purposely call write() and not print() here. This preserves
363 // compatibility with JDK 1.2.
364 write(Double.toString(dnum));
368 * This method prints an <code>Object</code> to the stream. The actual
369 * value printed is determined by calling the <code>String.valueOf()</code>
370 * method.
372 * @param obj The <code>Object</code> to print.
374 public void print(Object obj)
376 // We purposely call write() and not print() here. This preserves
377 // compatibility with JDK 1.2.
378 write(obj == null ? "null" : obj.toString());
382 * This is the system dependent line separator
384 private static final char[] line_separator
385 = System.getProperty("line.separator", "\n").toCharArray();
388 * This method prints a line separator sequence to the stream. The value
389 * printed is determined by the system property <xmp>line.separator</xmp>
390 * and is not necessarily the Unix '\n' newline character.
392 public void println()
394 synchronized (lock)
398 write(line_separator, 0, line_separator.length);
399 if (autoflush)
400 out.flush();
402 catch (IOException ex)
404 error = true;
410 * This methods prints a boolean value to the stream. <code>true</code>
411 * values are printed as "true" and <code>false</code> values are printed
412 * as "false".
414 * This method prints a line termination sequence after printing the value.
416 * @param bool The <code>boolean</code> value to print
418 public void println(boolean bool)
420 synchronized (lock)
422 print(bool);
423 println();
428 * This method prints an integer to the stream. The value printed is
429 * determined using the <code>String.valueOf()</code> method.
431 * This method prints a line termination sequence after printing the value.
433 * @param inum The <code>int</code> value to be printed
435 public void println(int inum)
437 synchronized (lock)
439 print(inum);
440 println();
445 * This method prints a long to the stream. The value printed is
446 * determined using the <code>String.valueOf()</code> method.
448 * This method prints a line termination sequence after printing the value.
450 * @param lnum The <code>long</code> value to be printed
452 public void println(long lnum)
454 synchronized (lock)
456 print(lnum);
457 println();
462 * This method prints a float to the stream. The value printed is
463 * determined using the <code>String.valueOf()</code> method.
465 * This method prints a line termination sequence after printing the value.
467 * @param fnum The <code>float</code> value to be printed
469 public void println(float fnum)
471 synchronized (lock)
473 print(fnum);
474 println();
479 * This method prints a double to the stream. The value printed is
480 * determined using the <code>String.valueOf()</code> method.
482 * This method prints a line termination sequence after printing the value.
484 * @param dnum The <code>double</code> value to be printed
486 public void println(double dnum)
488 synchronized (lock)
490 print(dnum);
491 println();
496 * This method prints an <code>Object</code> to the stream. The actual
497 * value printed is determined by calling the <code>String.valueOf()</code>
498 * method.
500 * This method prints a line termination sequence after printing the value.
502 * @param obj The <code>Object</code> to print.
504 public void println(Object obj)
506 synchronized (lock)
508 print(obj);
509 println();
514 * This method prints a <code>String</code> to the stream. The actual
515 * value printed depends on the system default encoding.
517 * This method prints a line termination sequence after printing the value.
519 * @param str The <code>String</code> to print.
521 public void println(String str)
523 synchronized (lock)
525 print(str);
526 println();
531 * This method prints a char to the stream. The actual value printed is
532 * determined by the character encoding in use.
534 * This method prints a line termination sequence after printing the value.
536 * @param ch The <code>char</code> value to be printed
538 public void println(char ch)
540 synchronized (lock)
542 print(ch);
543 println();
548 * This method prints an array of characters to the stream. The actual
549 * value printed depends on the system default encoding.
551 * This method prints a line termination sequence after printing the value.
553 * @param charArray The array of characters to print.
555 public void println(char[] charArray)
557 synchronized (lock)
559 print(charArray);
560 println();
565 * This method writes a single char to the stream.
567 * @param ch The char to be written, passed as a int
569 public void write(int ch)
573 out.write(ch);
575 catch (IOException ex)
577 error = true;
582 * This method writes <code>count</code> chars from the specified array
583 * starting at index <code>offset</code> into the array.
585 * @param charArray The array of chars to write
586 * @param offset The index into the array to start writing from
587 * @param count The number of chars to write
589 public void write(char[] charArray, int offset, int count)
593 out.write(charArray, offset, count);
595 catch (IOException ex)
597 error = true;
602 * This method writes <code>count</code> chars from the specified
603 * <code>String</code> to the output starting at character position
604 * <code>offset</code> into the <code>String</code>
606 * @param str The <code>String</code> to write chars from
607 * @param offset The offset into the <code>String</code> to start writing from
608 * @param count The number of chars to write.
610 public void write(String str, int offset, int count)
614 out.write(str, offset, count);
616 catch (IOException ex)
618 error = true;
623 * This method write all the chars in the specified array to the output.
625 * @param charArray The array of characters to write
627 public void write(char[] charArray)
629 write(charArray, 0, charArray.length);
633 * This method writes the contents of the specified <code>String</code>
634 * to the underlying stream.
636 * @param str The <code>String</code> to write
638 public void write(String str)
640 write(str, 0, str.length());
643 /** @since 1.5 */
644 public PrintWriter append(char c)
646 write(c);
647 return this;
650 /** @since 1.5 */
651 public PrintWriter append(CharSequence cs)
653 write(cs == null ? "null" : cs.toString());
654 return this;
657 /** @since 1.5 */
658 public PrintWriter append(CharSequence cs, int start, int end)
660 write(cs == null ? "null" : cs.subSequence(start, end).toString());
661 return this;
664 /** @since 1.5 */
665 public PrintWriter printf(String format, Object... args)
667 return format(format, args);
670 /** @since 1.5 */
671 public PrintWriter printf(Locale locale, String format, Object... args)
673 return format(locale, format, args);
676 /** @since 1.5 */
677 public PrintWriter format(String format, Object... args)
679 return format(Locale.getDefault(), format, args);
682 /** @since 1.5 */
683 public PrintWriter format(Locale locale, String format, Object... args)
685 Formatter f = new Formatter(this, locale);
686 f.format(format, args);
687 return this;