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

PrintStream

 
阅读更多

public class PrintStream extends FilterOutputStream implements Appendable,
		Closeable {

	private boolean autoFlush = false;
	private boolean trouble = false;
	private Formatter formatter;

	private BufferedWriter textOut;
	private OutputStreamWriter charOut;

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

	private PrintStream(boolean autoFlush, OutputStream out) {
		super(out);
		if (out == null)
			throw new NullPointerException("Null output stream");
		this.autoFlush = autoFlush;
	}

	private void init(OutputStreamWriter osw) {
		this.charOut = osw;
		this.textOut = new BufferedWriter(osw);
	}

	public PrintStream(OutputStream out, boolean autoFlush) {
		this(autoFlush, out);
		init(new OutputStreamWriter(this));
	}

	public PrintStream(OutputStream out, boolean autoFlush, String encoding)
			throws UnsupportedEncodingException {
		this(autoFlush, out);
		init(new OutputStreamWriter(this, encoding));
	}

	public PrintStream(String fileName) throws FileNotFoundException {
		this(false, new FileOutputStream(fileName));
		init(new OutputStreamWriter(this));
	}

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

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

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

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

	private boolean closing = false; /* To avoid recursive closing */

	public void close() {
		synchronized (this) {
			if (!closing) {
				closing = true;
				try {
					textOut.close();
					out.close();
				} catch (IOException x) {
					trouble = true;
				}
				textOut = null;
				charOut = null;
				out = null;
			}
		}
	}

	public boolean checkError() {
		if (out != null)
			flush();
		if (out instanceof java.io.PrintStream) {
			PrintStream ps = (PrintStream) out;
			return ps.checkError();
		}
		return trouble;
	}

	protected void setError() {
		trouble = true;
	}

	protected void clearError() {
		trouble = false;
	}

	/*
	 * Exception-catching, synchronized output operations, which also implement
	 * the write() methods of OutputStream
	 */

	public void write(int b) {
		try {
			synchronized (this) {
				ensureOpen();
				out.write(b);
				if ((b == '\n') && autoFlush)
					out.flush();
			}
		} catch (InterruptedIOException x) {
			Thread.currentThread().interrupt();
		} catch (IOException x) {
			trouble = true;
		}
	}

	public void write(byte buf[], int off, int len) {
		try {
			synchronized (this) {
				ensureOpen();
				out.write(buf, off, len);
				if (autoFlush)
					out.flush();
			}
		} catch (InterruptedIOException x) {
			Thread.currentThread().interrupt();
		} catch (IOException x) {
			trouble = true;
		}
	}

	/*
	 * The following private methods on the text- and character-output streams
	 * always flush the stream buffers, so that writes to the underlying byte
	 * stream occur as promptly as with the original PrintStream.
	 */

	private void write(char buf[]) {
		try {
			synchronized (this) {
				ensureOpen();
				textOut.write(buf);
				textOut.flushBuffer();
				charOut.flushBuffer();
				if (autoFlush) {
					for (int i = 0; i < buf.length; i++)
						if (buf[i] == '\n')
							out.flush();
				}
			}
		} catch (InterruptedIOException x) {
			Thread.currentThread().interrupt();
		} catch (IOException x) {
			trouble = true;
		}
	}

	private void write(String s) {
		try {
			synchronized (this) {
				ensureOpen();
				textOut.write(s);
				textOut.flushBuffer();
				charOut.flushBuffer();
				if (autoFlush && (s.indexOf('\n') >= 0))
					out.flush();
			}
		} catch (InterruptedIOException x) {
			Thread.currentThread().interrupt();
		} catch (IOException x) {
			trouble = true;
		}
	}

	private void newLine() {
		try {
			synchronized (this) {
				ensureOpen();
				textOut.newLine();
				textOut.flushBuffer();
				charOut.flushBuffer();
				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(String.valueOf(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 (this) {
			print(x);
			newLine();
		}
	}

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

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

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

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

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

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

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

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

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

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

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

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

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

	public PrintStream append(CharSequence csq, int start, int end) {
		CharSequence cs = (csq == null ? "null" : csq);
		write(cs.subSequence(start, end).toString());
		return this;
	}

	public PrintStream append(char c) {
		print(c);
		return this;
	}

}
 
分享到:
评论

相关推荐

    PrintStream,StringBuilder,Formatter

    在Java编程语言中,`PrintStream`, `StringBuilder` 和 `Formatter` 是三个非常重要的类,分别用于不同的输出处理。理解并熟练使用这三个类是提升Java编程能力的关键。 首先,我们来详细了解一下`PrintStream`。它...

    PrintStream 介绍_动力节点Java学院整理

    PrintStream 是打印输出流,它继承于FilterOutputStream。 PrintStream 是用来装饰其它输出流。它能为其他输出流添加了功能,使它们能够方便地打印各种数据值表示形式。 与其他输出流不同,PrintStream 永远不会抛出...

    java 输出流中的PrintStream 和 PrintWriter有什么区别

    java 输出流中的PrintStream 和 PrintWriter有什么区别

    java学习笔记--PrintStream分享.pdf

    Java学习笔记--PrintStream分享 PrintStream是一种输出流,能够将Java基本数据类型转换为系统预设编码下的字元,再输出至OutputStream中。在Java I/O流中,PrintStream是OutputStream的子类,主要用于将数据输出至...

    PrintStream和PrintWriter的区别简介

    "PrintStream和PrintWriter的区别简介" PrintStream和PrintWriter都是Java中的输出流类,都是用于将数据输出到目标设备的类,但是它们之间存在一些关键的区别。 首先,从构造方法上看,PrintStream和PrintWriter的...

    浅谈PrintStream和PrintWriter的区别和联系

    浅谈PrintStream和PrintWriter的区别和联系 PrintStream和PrintWriter都是Java中的输出流类,但它们之间存在着一些区别和联系。本篇文章将通过示例代码详细介绍这两者之间的区别和联系,希望能够对大家的学习或者...

    Java 中的Printstream介绍_动力节点Java学院整理

    Java 中的 PrintStream 介绍 PrintStream 是 Java 中的一种打印输出流,它继承自 FilterOutputStream。PrintStream 的主要功能是装饰其他输出流,使它们能够方便地打印各种数据值表示形式。 PrintStream 的特点是...

    PrintStream的用法2---马克-to-win java视频

    PrintStream的用法2---马克-to-win java视频

    PrintStream的用法1---马克-to-win java视频

    PrintStream的用法1---马克-to-win java视频的详细描述与介绍

    实验3 输入输出流的实验.doc

    本实验主要介绍了 Java 中的输入输出流,包括 DataInputStream、DataOutputStream、PrintStream 等类的使用,以及对象的序列化和反序列化。通过实验,我们可以掌握流的概念分类、字符串常用操作方法、流的构造和应用...

    JAVA_打印流例子

    在Java编程语言中,打印流(PrintStream)是用于输出文本信息的重要类,它属于`java.io`包。本文将深入探讨Java打印流的概念、用途、功能以及如何通过实例进行操作。 **一、打印流的概念** Java PrintStream 类提供...

    Arduino-PrintStream:一个简单的库,添加`std

    用法示例# include &lt; PrintStream&gt;void setup () { Serial. begin ( 115200 ); Serial &lt;&lt; " Hello, World! " &lt;&lt; endl; Serial &lt;&lt; F ( " Counting to 0xf in hexadecimal: " ) &lt;&lt; hex &lt;&...

    chatRoom 聊天室

    PrintStream ps = new PrintStream(s.getOutputStream()); /* 产生发消息的时刻 */ Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); /* 向客户端发消息,消息...

    Java软件开发实战 Java基础与案例开发详解 13-8 打印流 共6页.pdf

    - **PrintStream** 和 **PrintWriter** 都是打印流,它们提供了一系列的 `print` 和 `println` 方法,用于输出基本数据类型的数据,并将其格式化为字符串形式。 - **PrintStream** 和 **PrintWriter** 的输出操作...

    IBM面试题-IBM面试题

    ### IBM面试题解析 ...- `BufferedOutputStream`、`DataOutputStream` 和 `PrintStream` 都是 `FilterOutputStream` 的子类。 - `FilterOutputStream` 的构造函数通常接受一个 `OutputStream` 作为参数。

    java键盘输入输出练习.java

    对于不同数据类型的数据进行输入输出练习。过程中使用导入Scanner包,Scanner中创建Scanner类型的变量,内容详细易懂,适合Java新手练习

    java输入输出流实习题

    PrintStream printStream = new PrintStream(new FileOutputStream(logFile, true)); printStream.println("[当前时间] [" + logType + "] " + content); printStream.close(); ``` `readLog`方法则需要读取文件中...

    ACM中使用java(完整版)

    PrintStream out = new PrintStream(new FileOutputStream("pc2.estdout")); System.setOut(out); System.out.println("Hello World!"); out.close(); } } ``` 在上面的代码中,我们首先创建了一个 `...

    Android中使用文件存储实现手记应用的代码清单.pdf

    创建一个`PrintStream`实例,传入`FileOutputStream`,并设置其`true`参数,表示追加模式,这样每次保存都不会覆盖原有内容。在`try-catch-finally`块中,分别写入日期和手记内容,最后确保`PrintStream`在完成后被...

    java346888244

    import java.io.*; import java.util.Hashtable;...public chat (PrintStream sendmsg,Hashtable chatStream){ this.sendmsg = sendmsg; this.chatStream=chatStream; HttpStatus=new HttpStatusCodes(); }

Global site tag (gtag.js) - Google Analytics