java printwriter实例_java io系列25之 PrintWriter (字符打印输出流)

本文详细介绍了Java IO中的PrintWriter类,包括其构造方法、flush和close操作、错误处理机制以及各种print和println方法。此外,还探讨了如何通过PrintWriter进行格式化输出和字符集的处理,对于理解Java输出流的管理和控制具有重要意义。
摘要由CSDN通过智能技术生成

package java.io;

import java.util.Objects;

import java.util.Formatter;

import java.util.Locale;

import java.nio.charset.Charset;

import java.nio.charset.IllegalCharsetNameException;

import java.nio.charset.UnsupportedCharsetException;public classPrintWriter extends Writer {protected Writer out;//自动flush//所谓“自动flush”,就是每次执行print(), println(), write()函数,都会调用flush()函数;//而“不自动flush”,则需要我们手动调用flush()接口。

privatefinal boolean autoFlush;//PrintWriter是否右产生异常。当PrintWriter有异常产生时,会被本身捕获,并设置trouble为true

private boolean trouble = false;//用于格式化的对象

privateFormatter formatter;private PrintStream psOut = null;//行分割符

privatefinal String lineSeparator;//获取csn(字符集名字)对应的Chaset

private staticCharset toCharset(String csn)

throws UnsupportedEncodingException

{

Objects.requireNonNull(csn,"charsetName");try{returnCharset.forName(csn);

}catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {//UnsupportedEncodingException should be thrown

throw newUnsupportedEncodingException(csn);

}

}//将“Writer对象out”作为PrintWriter的输出流,默认不会自动flush,并且采用默认字符集。

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

}//将“Writer对象out”作为PrintWriter的输出流,autoFlush的flush模式,并且采用默认字符集。

public PrintWriter(Writer out, boolean autoFlush) {

super(out);this.out = out;this.autoFlush =autoFlush;

lineSeparator=java.security.AccessController.doPrivileged(new sun.security.action.GetPropertyAction("line.separator"));

}//将“输出流对象out”作为PrintWriter的输出流,不自动flush,并且采用默认字符集。

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

}//将“输出流对象out”作为PrintWriter的输出流,autoFlush的flush模式,并且采用默认字符集。

public PrintWriter(OutputStream out, boolean autoFlush) {//new OutputStreamWriter(out):将“字节类型的输出流”转换为“字符类型的输出流”//new BufferedWriter(...): 为输出流提供缓冲功能。

this(new BufferedWriter(new OutputStreamWriter(out)), autoFlush);//save print stream for error propagation

if (outinstanceof java.io.PrintStream) {

psOut= (PrintStream) out;

}

}//创建fileName对应的OutputStreamWriter,进而创建BufferedWriter对象;然后将该BufferedWriter作为PrintWriter的输出流,不自动flush,采用默认字符集。

publicPrintWriter(String fileName) throws FileNotFoundException {this(new BufferedWriter(new OutputStreamWriter(newFileOutputStream(fileName))),false);

}//创建fileName对应的OutputStreamWriter,进而创建BufferedWriter对象;然后将该BufferedWriter作为PrintWriter的输出流,不自动flush,采用字符集charset。

privatePrintWriter(Charset charset, File file)

throws FileNotFoundException

{this(new BufferedWriter(new OutputStreamWriter(newFileOutputStream(file), charset)),false);

}//创建fileName对应的OutputStreamWriter,进而创建BufferedWriter对象;然后将该BufferedWriter作为PrintWriter的输出流,不自动flush,采用csn字符集。

publicPrintWriter(String fileName, String csn)

throws FileNotFoundException, UnsupportedEncodingException

{this(toCharset(csn), newFile(fileName));

}//创建file对应的OutputStreamWriter,进而创建BufferedWriter对象;然后将该BufferedWriter作为PrintWriter的输出流,不自动flush,采用默认字符集。

publicPrintWriter(File file) throws FileNotFoundException {this(new BufferedWriter(new OutputStreamWriter(newFileOutputStream(file))),false);

}//创建file对应的OutputStreamWriter,进而创建BufferedWriter对象;然后将该BufferedWriter作为PrintWriter的输出流,不自动flush,采用csn字符集。

publicPrintWriter(File file, String csn)

throws FileNotFoundException, UnsupportedEncodingException

{this(toCharset(csn), file);

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

}//flush“PrintWriter输出流中的数据”。

public voidflush() {try{

synchronized (lock) {

ensureOpen();out.flush();

}

}catch(IOException x) {

trouble= true;

}

}public voidclose() {try{

synchronized (lock) {if (out == null)return;out.close();out = null;

}

}catch(IOException x) {

trouble= true;

}

}//flush“PrintWriter输出流缓冲中的数据”,并检查错误

publicboolean checkError() {if (out != null) {

flush();

}if (outinstanceof java.io.PrintWriter) {

PrintWriter pw= (PrintWriter) out;returnpw.checkError();

}else if (psOut != null) {returnpsOut.checkError();

}returntrouble;

}protected voidsetError() {

trouble= true;

}protected voidclearError() {

trouble= false;

}//将字符c写入到“PrintWriter输出流”中。c虽然是int类型,但实际只会写入一个字符

public void write(intc) {try{

synchronized (lock) {

ensureOpen();out.write(c);

}

}catch(InterruptedIOException x) {

Thread.currentThread().interrupt();

}catch(IOException x) {

trouble= true;

}

}//将“buf中从off开始的len个字符”写入到“PrintWriter输出流”中。

public void write(char buf[], int off, intlen) {try{

synchronized (lock) {

ensureOpen();out.write(buf, off, len);

}

}catch(InterruptedIOException x) {

Thread.currentThread().interrupt();

}catch(IOException x) {

trouble= true;

}

}//将“buf中的全部数据”写入到“PrintWriter输出流”中。

public void write(charbuf[]) {

write(buf,0, buf.length);

}//将“字符串s中从off开始的len个字符”写入到“PrintWriter输出流”中。

public void write(String s, int off, intlen) {try{

synchronized (lock) {

ensureOpen();out.write(s, off, len);

}

}catch(InterruptedIOException x) {

Thread.currentThread().interrupt();

}catch(IOException x) {

trouble= true;

}

}//将“字符串s”写入到“PrintWriter输出流”中。

public voidwrite(String s) {

write(s,0, s.length());

}//将“换行符”写入到“PrintWriter输出流”中。

private voidnewLine() {try{

synchronized (lock) {

ensureOpen();out.write(lineSeparator);if(autoFlush)out.flush();

}

}catch(InterruptedIOException x) {

Thread.currentThread().interrupt();

}catch(IOException x) {

trouble= true;

}

}//将“boolean数据对应的字符串”写入到“PrintWriter输出流”中,print实际调用的是write函数

public voidprint(boolean b) {

write(b? "true" : "false");

}//将“字符c对应的字符串”写入到“PrintWriter输出流”中,print实际调用的是write函数

public void print(charc) {

write(c);

}//将“int数据i对应的字符串”写入到“PrintWriter输出流”中,print实际调用的是write函数

public void print(inti) {

write(String.valueOf(i));

}//将“long型数据l对应的字符串”写入到“PrintWriter输出流”中,print实际调用的是write函数

public void print(longl) {

write(String.valueOf(l));

}//将“float数据f对应的字符串”写入到“PrintWriter输出流”中,print实际调用的是write函数

public void print(floatf) {

write(String.valueOf(f));

}//将“double数据d对应的字符串”写入到“PrintWriter输出流”中,print实际调用的是write函数

public void print(doubled) {

write(String.valueOf(d));

}//将“字符数组s”写入到“PrintWriter输出流”中,print实际调用的是write函数

public void print(chars[]) {

write(s);

}//将“字符串数据s”写入到“PrintWriter输出流”中,print实际调用的是write函数

public voidprint(String s) {if (s == null) {

s= "null";

}

write(s);

}//将“对象obj对应的字符串”写入到“PrintWriter输出流”中,print实际调用的是write函数

public voidprint(Object obj) {

write(String.valueOf(obj));

}//将“换行符”写入到“PrintWriter输出流”中,println实际调用的是write函数

public voidprintln() {

newLine();

}//将“boolean数据对应的字符串+换行符”写入到“PrintWriter输出流”中,println实际调用的是write函数

public voidprintln(boolean x) {

synchronized (lock) {

print(x);

println();

}

}//将“字符x对应的字符串+换行符”写入到“PrintWriter输出流”中,println实际调用的是write函数

public void println(charx) {

synchronized (lock) {

print(x);

println();

}

}//将“int数据对应的字符串+换行符”写入到“PrintWriter输出流”中,println实际调用的是write函数

public void println(intx) {

synchronized (lock) {

print(x);

println();

}

}//将“long数据对应的字符串+换行符”写入到“PrintWriter输出流”中,println实际调用的是write函数

public void println(longx) {

synchronized (lock) {

print(x);

println();

}

}//将“float数据对应的字符串+换行符”写入到“PrintWriter输出流”中,println实际调用的是write函数

public void println(floatx) {

synchronized (lock) {

print(x);

println();

}

}//将“double数据对应的字符串+换行符”写入到“PrintWriter输出流”中,println实际调用的是write函数

public void println(doublex) {

synchronized (lock) {

print(x);

println();

}

}//将“字符数组x+换行符”写入到“PrintWriter输出流”中,println实际调用的是write函数

public void println(charx[]) {

synchronized (lock) {

print(x);

println();

}

}//将“字符串x+换行符”写入到“PrintWriter输出流”中,println实际调用的是write函数

public voidprintln(String x) {

synchronized (lock) {

print(x);

println();

}

}//将“对象o对应的字符串+换行符”写入到“PrintWriter输出流”中,println实际调用的是write函数

public voidprintln(Object x) {

String s=String.valueOf(x);

synchronized (lock) {

print(s);

println();

}

}//将“数据args”根据“默认Locale值(区域属性)”按照format格式化,并写入到“PrintWriter输出流”中

publicPrintWriter printf(String format, Object ... args) {returnformat(format, args);

}//将“数据args”根据“Locale值(区域属性)”按照format格式化,并写入到“PrintWriter输出流”中

publicPrintWriter printf(Locale l, String format, Object ... args) {returnformat(l, format, args);

}//根据“默认的Locale值(区域属性)”来格式化数据

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

}//根据“Locale值(区域属性)”来格式化数据

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

}//将“字符序列的全部字符”追加到“PrintWriter输出流中”

publicPrintWriter append(CharSequence csq) {if (csq == null)

write("null");elsewrite(csq.toString());return this;

}//将“字符序列从start(包括)到end(不包括)的全部字符”追加到“PrintWriter输出流中”

public PrintWriter append(CharSequence csq, int start, intend) {

CharSequence cs= (csq == null ? "null": csq);

write(cs.subSequence(start, end).toString());return this;

}//将“字符c”追加到“PrintWriter输出流中”

public PrintWriter append(charc) {

write(c);return this;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值