?? printwriter.java
字號:
/* * @(#)PrintWriter.java 1.32 06/10/13 * * Copyright 1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package java.io;/** * Print formatted representations of objects to a text-output stream. This * class implements all of the print methods found in PrintStream. It does not * contain methods for writing raw bytes, for which a program should use * unencoded byte streams. * * <p> Unlike the PrintStream class, if automatic flushing is enabled it will * be done only when one of the println() methods is invoked, rather than * whenever a newline character happens to be output. The println() methods * use the platform's own notion of line separator rather than the newline * character. * * <p> Methods in this class never throw I/O exceptions. The client may * inquire as to whether any errors have occurred by invoking checkError(). * * @version 1.24, 02/02/00 * @author Mark Reinhold * @since JDK1.1 */public class PrintWriter extends Writer { /** * The underlying character-output stream of this * <code>PrintWriter</code>. * * @since 1.2 */ protected Writer out; private boolean autoFlush = false; private boolean trouble = false; /** * Line separator string. This is the value of the line.separator * property at the moment that the stream was created. */ private String lineSeparator; /** * Create a new PrintWriter, without automatic line flushing. * * @param out A character-output stream */ public PrintWriter (Writer out) { this(out, false); } /** * Create a new PrintWriter. * * @param out A character-output stream * @param autoFlush A boolean; if true, the println() methods will flush * the output buffer */ public PrintWriter(Writer out, boolean autoFlush) { super(out); this.out = out; this.autoFlush = autoFlush; lineSeparator = (String) java.security.AccessController.doPrivileged( new sun.security.action.GetPropertyAction("line.separator")); } /** * Create a new PrintWriter, without automatic line flushing, from an * existing OutputStream. This convenience constructor creates the * necessary intermediate OutputStreamWriter, which will convert characters * into bytes using the default character encoding. * * @param out An output stream * * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream) */ public PrintWriter(OutputStream out) { this(out, false); } /** * Create a new PrintWriter from an existing OutputStream. This * convenience constructor creates the necessary intermediate * OutputStreamWriter, which will convert characters into bytes using the * default character encoding. * * @param out An output stream * @param autoFlush A boolean; if true, the println() methods will flush * the output buffer * * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream) */ public PrintWriter(OutputStream out, boolean autoFlush) { this(new BufferedWriter(new OutputStreamWriter(out)), autoFlush); } /** Check to make sure that the stream has not been closed */ private void ensureOpen() throws IOException { if (out == null) throw new IOException("Stream closed"); } /** * Flush the stream. * @see #checkError() */ public void flush() { try { synchronized (lock) { ensureOpen(); out.flush(); } } catch (IOException x) { trouble = true; } } /** * Close the stream. * @see #checkError() */ public void close() { try { synchronized (lock) { if (out == null) return; out.close(); out = null; } } catch (IOException x) { trouble = true; } } /** * Flush the stream if it's not closed and check its error state. * Errors are cumulative; once the stream encounters an error, this * routine will return true on all successive calls. * * @return True if the print stream has encountered an error, either on the * underlying output stream or during a format conversion. */ public boolean checkError() { if (out != null) flush(); return trouble; } /** Indicate that an error has occurred. */ protected void setError() { trouble = true; } /* * Exception-catching, synchronized output operations, * which also implement the write() methods of Writer */ /** * Write a single character. * @param c int specifying a character to be written. */ public void write(int c) { try { synchronized (lock) { ensureOpen(); out.write(c); } } catch (InterruptedIOException x) { Thread.currentThread().interrupt(); } catch (IOException x) { trouble = true; } } /** * Write a portion of an array of characters. * @param buf Array of characters * @param off Offset from which to start writing characters * @param len Number of characters to write */ public void write(char buf[], int off, int len) { try { synchronized (lock) { ensureOpen(); out.write(buf, off, len); } } catch (InterruptedIOException x) { Thread.currentThread().interrupt(); } catch (IOException x) { trouble = true; } } /** * Write an array of characters. This method cannot be inherited from the * Writer class because it must suppress I/O exceptions. * @param buf Array of characters to be written */ public void write(char buf[]) { write(buf, 0, buf.length); } /** * Write a portion of a string. * @param s A String * @param off Offset from which to start writing characters * @param len Number of characters to write */ public void write(String s, int off, int len) { try { synchronized (lock) { ensureOpen(); out.write(s, off, len); } } catch (InterruptedIOException x) { Thread.currentThread().interrupt(); } catch (IOException x) { trouble = true; } } /** * Write a string. This method cannot be inherited from the Writer class * because it must suppress I/O exceptions. * @param s String to be written */ public void write(String s) { write(s, 0, s.length()); } private void newLine() { try { synchronized (lock) { ensureOpen(); out.write(lineSeparator); if (autoFlush) out.flush(); } }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -