`
默默的小熊
  • 浏览: 227351 次
社区版块
存档分类
最新评论

PrintWriter

 
阅读更多

public class PrintWriter extends Writer {

	protected Writer out;

	private boolean autoFlush = false;
	private boolean trouble = false;
	private Formatter formatter;
	private PrintStream psOut = null;

	private String lineSeparator;

	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"));
	}

	public PrintWriter(Writer out) {
		this(out, false);
	}

	public PrintWriter(OutputStream out) {
		this(out, false);
	}

	public PrintWriter(OutputStream out, boolean autoFlush) {
		this(new BufferedWriter(new OutputStreamWriter(out)), autoFlush);

		// save print stream for error propagation
		if (out instanceof java.io.PrintStream) {
			psOut = (PrintStream) out;
		}
	}

	//创建一个PrintWriter, 没有自动行刷新,有一个指定的文件名。
	//这个便利的构造器创建一个中介OutputStreamWriter,它可以编码字符
	//使用defaultCharset()方法
	public PrintWriter(String fileName) throws FileNotFoundException {
		this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
				fileName))), false);
	}

	public PrintWriter(String fileName, String csn)
			throws FileNotFoundException, UnsupportedEncodingException {
		this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
				fileName), csn)), false);
	}

	public PrintWriter(File file) throws FileNotFoundException {
		this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
				file))), false);
	}

	public PrintWriter(File file, String csn) throws FileNotFoundException,
			UnsupportedEncodingException {
		this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
				file), csn)), false);
	}

	private void ensureOpen() throws IOException {
		if (out == null)
			throw new IOException("Stream closed");
	}

	public void flush() {
		try {
			synchronized (lock) {
				ensureOpen();
				out.flush();
			}
		} catch (IOException x) {
			trouble = true;
		}
	}

	public void close() {
		try {
			synchronized (lock) {
				if (out == null)
					return;
				out.close();
				out = null;
			}
		} catch (IOException x) {
			trouble = true;
		}
	}

	public boolean checkError() {
		if (out != null) {
			flush();
		}
		if (out instanceof java.io.PrintWriter) {
			PrintWriter pw = (PrintWriter) out;
			return pw.checkError();
		} else if (psOut != null) {
			return psOut.checkError();
		}
		return trouble;
	}

	protected void setError() {
		trouble = true;
	}

	protected void clearError() {
		trouble = false;
	}

	public void write(int c) {
		try {
			synchronized (lock) {
				ensureOpen();
				out.write(c);
			}
		} catch (InterruptedIOException x) {
			Thread.currentThread().interrupt();
		} catch (IOException x) {
			trouble = true;
		}
	}

	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;
		}
	}

	public void write(char buf[]) {
		write(buf, 0, buf.length);
	}

	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;
		}
	}

	public void write(String s) {
		write(s, 0, s.length());
	}

	private void newLine() {
		try {
			synchronized (lock) {
				ensureOpen();
				out.write(lineSeparator);
				if (autoFlush)
					out.flush();
			}
		} catch (InterruptedIOException x) {
			Thread.currentThread().interrupt();
		} catch (IOException x) {
			trouble = true;
		}
	}

	/* Methods that do not terminate lines */

	public void print(boolean b) {
		write(b ? "true" : "false");
	}

	public void print(char c) {
		write(c);
	}

	public void print(int i) {
		write(String.valueOf(i));
	}

	public void print(long l) {
		write(String.valueOf(l));
	}

	public void print(float f) {
		write(String.valueOf(f));
	}

	public void print(double d) {
		write(String.valueOf(d));
	}

	public void print(char s[]) {
		write(s);
	}

	public void print(String s) {
		if (s == null) {
			s = "null";
		}
		write(s);
	}

	public void print(Object obj) {
		write(String.valueOf(obj));
	}

	/* Methods that do terminate lines */

	public void println() {
		newLine();
	}

	public void println(boolean x) {
		synchronized (lock) {
			print(x);
			println();
		}
	}

	public void println(char x) {
		synchronized (lock) {
			print(x);
			println();
		}
	}

	public void println(int x) {
		synchronized (lock) {
			print(x);
			println();
		}
	}

	public void println(long x) {
		synchronized (lock) {
			print(x);
			println();
		}
	}

	public void println(float x) {
		synchronized (lock) {
			print(x);
			println();
		}
	}

	public void println(double x) {
		synchronized (lock) {
			print(x);
			println();
		}
	}

	public void println(char x[]) {
		synchronized (lock) {
			print(x);
			println();
		}
	}

	public void println(String x) {
		synchronized (lock) {
			print(x);
			println();
		}
	}

	public void println(Object x) {
		String s = String.valueOf(x);
		synchronized (lock) {
			print(s);
			println();
		}
	}

	public PrintWriter printf(String format, Object... args) {
		return format(format, args);
	}

	public PrintWriter printf(Locale l, String format, Object... args) {
		return format(l, format, args);
	}

	public PrintWriter format(String format, Object... args) {
		try {
			synchronized (lock) {
				ensureOpen();
				if ((formatter == null)
						|| (formatter.locale() != Locale.getDefault()))
					formatter = new Formatter(this);
				formatter.format(Locale.getDefault(), format, args);
				if (autoFlush)
					out.flush();
			}
		} catch (InterruptedIOException x) {
			Thread.currentThread().interrupt();
		} catch (IOException x) {
			trouble = true;
		}
		return this;
	}

	public PrintWriter format(Locale l, String format, Object... args) {
		try {
			synchronized (lock) {
				ensureOpen();
				if ((formatter == null) || (formatter.locale() != l))
					formatter = new Formatter(this, l);
				formatter.format(l, format, args);
				if (autoFlush)
					out.flush();
			}
		} catch (InterruptedIOException x) {
			Thread.currentThread().interrupt();
		} catch (IOException x) {
			trouble = true;
		}
		return this;
	}

	public PrintWriter append(CharSequence csq) {
		if (csq == null)
			write("null");
		else
			write(csq.toString());
		return this;
	}

	public PrintWriter append(CharSequence csq, int start, int end) {
		CharSequence cs = (csq == null ? "null" : csq);
		write(cs.subSequence(start, end).toString());
		return this;
	}
	public PrintWriter append(char c) {
		write(c);
		return this;
	}
}
 
分享到:
评论
1 楼 hzxlb910 2013-01-08  
你也搞点注释嘛,

相关推荐

Global site tag (gtag.js) - Google Analytics