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

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  
你也搞点注释嘛,

相关推荐

    Java Scanner、File、PrintWriter使用实例

    Java Scanner File PrintWriter使用实例

    java写入txt PrintWriter FileOutputStream

    本篇文章将详细讲解如何利用`PrintWriter`类和`FileOutputStream`输出流来实现这个功能。 一、使用`PrintWriter`类 `PrintWriter`是Java.io包中的一个类,它提供了方便的打印方法,如`println()`,用于向输出流...

    PrintWriter的用法---马克-to-win java视频

    PrintWriter的用法---马克-to-win java视频 打印输出流

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

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

    PrintStream和PrintWriter的区别简介

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

    浅谈PrintStream和PrintWriter的区别和联系

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

    【公益知识库zscc.club】51-IO流(PrintWriter).avi

    【公益知识库zscc.club】51-IO流(PrintWriter).avi

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

    Java中的PrintWriter介绍 PrintWriter是Java中的一种字符类型的打印输出流,继承于Writer。它用于向文本输出流打印对象的格式化表示形式。PrintWriter提供了多种构造方法,例如PrintWriter(OutputStream out)、...

    Java servlet 使用 PrintWriter 时的编码与乱码的示例代码

    然而,处理编码问题时,PrintWriter的行为可能会导致乱码,特别是在处理非ASCII字符(如中文字符)时。 首先,我们需要了解PrintWriter的默认编码。当没有明确指定编码时,PrintWriter并不会使用`Charset.default...

    WriteTextFiles.rar

    在Java编程语言中,`PrintWriter`和`FileWriter`都是用于向文件写入文本的类,它们在处理文本输出时各有特点。本教程将详细探讨这两个类的使用方法、异同以及如何在实际编程中结合运用。 首先,让我们了解`...

    File-Output-w-PrintWriter:一个演示 PrintWriter 类的简单程序。 提示用户输入他们有多少朋友

    File-Output-w-PrintWriter 一个演示 PrintWriter 类的简单程序。 系统会提示用户输入文件名以及他们有多少朋友。 将使用 PrintWriter 类创建一个文件。 然后,FOR 循环将根据用户的好友数量重复次数。 每次循环时...

    Android程序静默安装安装后重新启动APP的方法

    3. **执行安装命令**:使用`pm install`命令进行静默安装,加上`-r`参数表示更新已有应用,完整的命令类似`PrintWriter.println("pm install -r " + apkPath)`。 然而,静默安装后,原始应用进程可能会被系统终止,...

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

    PrintWriter和PrintStream类似,但是PrintWriter可以接受Writer对象作为输出的对象,使得它具有更大的灵活性。 在Java I/O流中,存在两种类型的流:字节流和字符流。字节流是指InputStream/OutputStream及其子类,...

    TCP编程——网络编程详细源码

    PrintWriter printWriter = new PrintWriter(clientOutput, true); printWriter.println("OVER"); } else { // 输入正方形边长,并输入其边长及面积 double side = Double.parseDouble(strLine); ...

    PrintWriterPrinter.rar_android

    在Android开发中,`PrintWriterPrinter.rar_android`这个压缩包涉及到的是如何在Android系统中实现一个基于`java.io.PrintWriter`的`android.util.Printer`。`android.util.Printer`是Android SDK提供的一种接口,...

    Java中文件的输入与输出.pdf

    本文将深入探讨Java中如何进行文件的读取和写入操作,主要涉及`InputStream`、`OutputStream`及其相关的子类,如`BufferedReader`和`PrintWriter`。 `InputStream`和`OutputStream`是Java I/O流的基础,它们是所有...

    Java中文件的输入与输出实例.pdf

    本实例详细介绍了如何在Java中实现这一功能,主要涉及了`BufferedReader`和`PrintWriter`类的使用,以及文件路径的设置。 首先,声明`BufferedReader`和`PrintWriter`对象。`BufferedReader`用于读取文件,而`...

    Java中控制台的输入与输出.pdf

    private static PrintWriter stdOut = new PrintWriter(System.out, true); private static PrintWriter stdErr = new PrintWriter(System.out, true); ``` 这里的`true`参数表示自动刷新,这意味着每次调用`...

    高级java java 代码

    高级javaimport java.io.BufferedReader; import java.io.... PrintWriter os =new PrintWriter(socket.getOutputStream()); String readline; readline =sin.readLine(); while(!readline.equals("bye")){

    Java Socket TCP 通信

    printWriter = new PrintWriter(clientSocket.getOutputStream(), true); // 初始化输入流 bufferedReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); // 发送消息到...

Global site tag (gtag.js) - Google Analytics