zoukankan      html  css  js  c++  java
  • java io系列25之 PrintWriter (字符打印输出流)

    更多内容请参考:java io系列01之 "目录" 

    PrintWriter 介绍

    PrintWriter 是字符类型的打印输出流,它继承于Writer。
    PrintStream 用于向文本输出流打印对象的格式化表示形式。它实现在 PrintStream 中的所有 print 方法。它不包含用于写入原始字节的方法,对于这些字节,程序应该使用未编码的字节流进行写入。

    PrintWriter 函数列表

    复制代码
    PrintWriter(OutputStream out)
    PrintWriter(OutputStream out, boolean autoFlush)
    PrintWriter(Writer wr)
    PrintWriter(Writer wr, boolean autoFlush)
    PrintWriter(File file)
    PrintWriter(File file, String csn)
    PrintWriter(String fileName)
    PrintWriter(String fileName, String csn)
    
    PrintWriter     append(char c)
    PrintWriter     append(CharSequence csq, int start, int end)
    PrintWriter     append(CharSequence csq)
    boolean     checkError()
    void     close()
    void     flush()
    PrintWriter     format(Locale l, String format, Object... args)
    PrintWriter     format(String format, Object... args)
    void     print(float fnum)
    void     print(double dnum)
    void     print(String str)
    void     print(Object obj)
    void     print(char ch)
    void     print(char[] charArray)
    void     print(long lnum)
    void     print(int inum)
    void     print(boolean bool)
    PrintWriter     printf(Locale l, String format, Object... args)
    PrintWriter     printf(String format, Object... args)
    void     println()
    void     println(float f)
    void     println(int i)
    void     println(long l)
    void     println(Object obj)
    void     println(char[] chars)
    void     println(String str)
    void     println(char c)
    void     println(double d)
    void     println(boolean b)
    void     write(char[] buf, int offset, int count)
    void     write(int oneChar)
    void     write(char[] buf)
    void     write(String str, int offset, int count)
    void     write(String str)
    复制代码

    PrintWriter 源码

    复制代码
      1 package java.io;
      2 
      3 import java.util.Objects;
      4 import java.util.Formatter;
      5 import java.util.Locale;
      6 import java.nio.charset.Charset;
      7 import java.nio.charset.IllegalCharsetNameException;
      8 import java.nio.charset.UnsupportedCharsetException;
      9 
     10 public class PrintWriter extends Writer {
     11 
     12     protected Writer out;
     13 
     14     // 自动flush
     15     // 所谓“自动flush”,就是每次执行print(), println(), write()函数,都会调用flush()函数;
     16     // 而“不自动flush”,则需要我们手动调用flush()接口。
     17     private final boolean autoFlush;
     18     // PrintWriter是否右产生异常。当PrintWriter有异常产生时,会被本身捕获,并设置trouble为true
     19     private boolean trouble = false;
     20     // 用于格式化的对象
     21     private Formatter formatter;
     22     private PrintStream psOut = null;
     23 
     24     // 行分割符
     25     private final String lineSeparator;
     26 
     27     // 获取csn(字符集名字)对应的Chaset
     28     private static Charset toCharset(String csn)
     29         throws UnsupportedEncodingException
     30     {
     31         Objects.requireNonNull(csn, "charsetName");
     32         try {
     33             return Charset.forName(csn);
     34         } catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
     35             // UnsupportedEncodingException should be thrown
     36             throw new UnsupportedEncodingException(csn);
     37         }
     38     }
     39 
     40     // 将“Writer对象out”作为PrintWriter的输出流,默认不会自动flush,并且采用默认字符集。
     41     public PrintWriter (Writer out) {
     42         this(out, false);
     43     }
     44 
     45     // 将“Writer对象out”作为PrintWriter的输出流,autoFlush的flush模式,并且采用默认字符集。
     46     public PrintWriter(Writer out, boolean autoFlush) {
     47         super(out);
     48         this.out = out;
     49         this.autoFlush = autoFlush;
     50         lineSeparator = java.security.AccessController.doPrivileged(
     51             new sun.security.action.GetPropertyAction("line.separator"));
     52     }
     53 
     54     // 将“输出流对象out”作为PrintWriter的输出流,不自动flush,并且采用默认字符集。
     55     public PrintWriter(OutputStream out) {
     56         this(out, false);
     57     }
     58 
     59     // 将“输出流对象out”作为PrintWriter的输出流,autoFlush的flush模式,并且采用默认字符集。
     60     public PrintWriter(OutputStream out, boolean autoFlush) {
     61         // new OutputStreamWriter(out):将“字节类型的输出流”转换为“字符类型的输出流”
     62         // new BufferedWriter(...): 为输出流提供缓冲功能。
     63         this(new BufferedWriter(new OutputStreamWriter(out)), autoFlush);
     64 
     65         // save print stream for error propagation
     66         if (out instanceof java.io.PrintStream) {
     67             psOut = (PrintStream) out;
     68         }
     69     }
     70 
     71     // 创建fileName对应的OutputStreamWriter,进而创建BufferedWriter对象;然后将该BufferedWriter作为PrintWriter的输出流,不自动flush,采用默认字符集。
     72     public PrintWriter(String fileName) throws FileNotFoundException {
     73         this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))),
     74              false);
     75     }
     76 
     77     // 创建fileName对应的OutputStreamWriter,进而创建BufferedWriter对象;然后将该BufferedWriter作为PrintWriter的输出流,不自动flush,采用字符集charset。
     78     private PrintWriter(Charset charset, File file)
     79         throws FileNotFoundException
     80     {
     81         this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset)),
     82              false);
     83     }
     84 
     85     // 创建fileName对应的OutputStreamWriter,进而创建BufferedWriter对象;然后将该BufferedWriter作为PrintWriter的输出流,不自动flush,采用csn字符集。
     86     public PrintWriter(String fileName, String csn)
     87         throws FileNotFoundException, UnsupportedEncodingException
     88     {
     89         this(toCharset(csn), new File(fileName));
     90     }
     91 
     92     // 创建file对应的OutputStreamWriter,进而创建BufferedWriter对象;然后将该BufferedWriter作为PrintWriter的输出流,不自动flush,采用默认字符集。
     93     public PrintWriter(File file) throws FileNotFoundException {
     94         this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))),
     95              false);
     96     }
     97 
     98     // 创建file对应的OutputStreamWriter,进而创建BufferedWriter对象;然后将该BufferedWriter作为PrintWriter的输出流,不自动flush,采用csn字符集。
     99     public PrintWriter(File file, String csn)
    100         throws FileNotFoundException, UnsupportedEncodingException
    101     {
    102         this(toCharset(csn), file);
    103     }
    104 
    105     private void ensureOpen() throws IOException {
    106         if (out == null)
    107             throw new IOException("Stream closed");
    108     }
    109 
    110     // flush“PrintWriter输出流中的数据”。
    111     public void flush() {
    112         try {
    113             synchronized (lock) {
    114                 ensureOpen();
    115                 out.flush();
    116             }
    117         }
    118         catch (IOException x) {
    119             trouble = true;
    120         }
    121     }
    122 
    123     public void close() {
    124         try {
    125             synchronized (lock) {
    126                 if (out == null)
    127                     return;
    128                 out.close();
    129                 out = null;
    130             }
    131         }
    132         catch (IOException x) {
    133             trouble = true;
    134         }
    135     }
    136 
    137     // flush“PrintWriter输出流缓冲中的数据”,并检查错误
    138     public boolean checkError() {
    139         if (out != null) {
    140             flush();
    141         }
    142         if (out instanceof java.io.PrintWriter) {
    143             PrintWriter pw = (PrintWriter) out;
    144             return pw.checkError();
    145         } else if (psOut != null) {
    146             return psOut.checkError();
    147         }
    148         return trouble;
    149     }
    150 
    151     protected void setError() {
    152         trouble = true;
    153     }
    154 
    155     protected void clearError() {
    156         trouble = false;
    157     }
    158 
    159     // 将字符c写入到“PrintWriter输出流”中。c虽然是int类型,但实际只会写入一个字符
    160     public void write(int c) {
    161         try {
    162             synchronized (lock) {
    163                 ensureOpen();
    164                 out.write(c);
    165             }
    166         }
    167         catch (InterruptedIOException x) {
    168             Thread.currentThread().interrupt();
    169         }
    170         catch (IOException x) {
    171             trouble = true;
    172         }
    173     }
    174 
    175     // 将“buf中从off开始的len个字符”写入到“PrintWriter输出流”中。
    176     public void write(char buf[], int off, int len) {
    177         try {
    178             synchronized (lock) {
    179                 ensureOpen();
    180                 out.write(buf, off, len);
    181             }
    182         }
    183         catch (InterruptedIOException x) {
    184             Thread.currentThread().interrupt();
    185         }
    186         catch (IOException x) {
    187             trouble = true;
    188         }
    189     }
    190 
    191     // 将“buf中的全部数据”写入到“PrintWriter输出流”中。
    192     public void write(char buf[]) {
    193         write(buf, 0, buf.length);
    194     }
    195 
    196     // 将“字符串s中从off开始的len个字符”写入到“PrintWriter输出流”中。
    197     public void write(String s, int off, int len) {
    198         try {
    199             synchronized (lock) {
    200                 ensureOpen();
    201                 out.write(s, off, len);
    202             }
    203         }
    204         catch (InterruptedIOException x) {
    205             Thread.currentThread().interrupt();
    206         }
    207         catch (IOException x) {
    208             trouble = true;
    209         }
    210     }
    211 
    212     // 将“字符串s”写入到“PrintWriter输出流”中。
    213     public void write(String s) {
    214         write(s, 0, s.length());
    215     }
    216 
    217     // 将“换行符”写入到“PrintWriter输出流”中。
    218     private void newLine() {
    219         try {
    220             synchronized (lock) {
    221                 ensureOpen();
    222                 out.write(lineSeparator);
    223                 if (autoFlush)
    224                     out.flush();
    225             }
    226         }
    227         catch (InterruptedIOException x) {
    228             Thread.currentThread().interrupt();
    229         }
    230         catch (IOException x) {
    231             trouble = true;
    232         }
    233     }
    234 
    235     // 将“boolean数据对应的字符串”写入到“PrintWriter输出流”中,print实际调用的是write函数
    236     public void print(boolean b) {
    237         write(b ? "true" : "false");
    238     }
    239 
    240     // 将“字符c对应的字符串”写入到“PrintWriter输出流”中,print实际调用的是write函数
    241     public void print(char c) {
    242         write(c);
    243     }
    244 
    245     // 将“int数据i对应的字符串”写入到“PrintWriter输出流”中,print实际调用的是write函数
    246     public void print(int i) {
    247         write(String.valueOf(i));
    248     }
    249 
    250     // 将“long型数据l对应的字符串”写入到“PrintWriter输出流”中,print实际调用的是write函数
    251     public void print(long l) {
    252         write(String.valueOf(l));
    253     }
    254 
    255     // 将“float数据f对应的字符串”写入到“PrintWriter输出流”中,print实际调用的是write函数
    256     public void print(float f) {
    257         write(String.valueOf(f));
    258     }
    259 
    260     // 将“double数据d对应的字符串”写入到“PrintWriter输出流”中,print实际调用的是write函数
    261     public void print(double d) {
    262         write(String.valueOf(d));
    263     }
    264 
    265     // 将“字符数组s”写入到“PrintWriter输出流”中,print实际调用的是write函数
    266     public void print(char s[]) {
    267         write(s);
    268     }
    269 
    270     // 将“字符串数据s”写入到“PrintWriter输出流”中,print实际调用的是write函数
    271     public void print(String s) {
    272         if (s == null) {
    273             s = "null";
    274         }
    275         write(s);
    276     }
    277 
    278     // 将“对象obj对应的字符串”写入到“PrintWriter输出流”中,print实际调用的是write函数
    279     public void print(Object obj) {
    280         write(String.valueOf(obj));
    281     }
    282 
    283     // 将“换行符”写入到“PrintWriter输出流”中,println实际调用的是write函数
    284     public void println() {
    285         newLine();
    286     }
    287 
    288     // 将“boolean数据对应的字符串+换行符”写入到“PrintWriter输出流”中,println实际调用的是write函数
    289     public void println(boolean x) {
    290         synchronized (lock) {
    291             print(x);
    292             println();
    293         }
    294     }
    295 
    296     // 将“字符x对应的字符串+换行符”写入到“PrintWriter输出流”中,println实际调用的是write函数
    297     public void println(char x) {
    298         synchronized (lock) {
    299             print(x);
    300             println();
    301         }
    302     }
    303 
    304     // 将“int数据对应的字符串+换行符”写入到“PrintWriter输出流”中,println实际调用的是write函数
    305     public void println(int x) {
    306         synchronized (lock) {
    307             print(x);
    308             println();
    309         }
    310     }
    311 
    312     // 将“long数据对应的字符串+换行符”写入到“PrintWriter输出流”中,println实际调用的是write函数
    313     public void println(long x) {
    314         synchronized (lock) {
    315             print(x);
    316             println();
    317         }
    318     }
    319 
    320     // 将“float数据对应的字符串+换行符”写入到“PrintWriter输出流”中,println实际调用的是write函数
    321     public void println(float x) {
    322         synchronized (lock) {
    323             print(x);
    324             println();
    325         }
    326     }
    327 
    328     // 将“double数据对应的字符串+换行符”写入到“PrintWriter输出流”中,println实际调用的是write函数
    329     public void println(double x) {
    330         synchronized (lock) {
    331             print(x);
    332             println();
    333         }
    334     }
    335 
    336     // 将“字符数组x+换行符”写入到“PrintWriter输出流”中,println实际调用的是write函数
    337     public void println(char x[]) {
    338         synchronized (lock) {
    339             print(x);
    340             println();
    341         }
    342     }
    343 
    344     // 将“字符串x+换行符”写入到“PrintWriter输出流”中,println实际调用的是write函数
    345     public void println(String x) {
    346         synchronized (lock) {
    347             print(x);
    348             println();
    349         }
    350     }
    351 
    352     // 将“对象o对应的字符串+换行符”写入到“PrintWriter输出流”中,println实际调用的是write函数
    353     public void println(Object x) {
    354         String s = String.valueOf(x);
    355         synchronized (lock) {
    356             print(s);
    357             println();
    358         }
    359     }
    360 
    361     // 将“数据args”根据“默认Locale值(区域属性)”按照format格式化,并写入到“PrintWriter输出流”中
    362     public PrintWriter printf(String format, Object ... args) {
    363         return format(format, args);
    364     }
    365 
    366     // 将“数据args”根据“Locale值(区域属性)”按照format格式化,并写入到“PrintWriter输出流”中
    367     public PrintWriter printf(Locale l, String format, Object ... args) {
    368         return format(l, format, args);
    369     }
    370 
    371     // 根据“默认的Locale值(区域属性)”来格式化数据
    372     public PrintWriter format(String format, Object ... args) {
    373         try {
    374             synchronized (lock) {
    375                 ensureOpen();
    376                 if ((formatter == null)
    377                     || (formatter.locale() != Locale.getDefault()))
    378                     formatter = new Formatter(this);
    379                 formatter.format(Locale.getDefault(), format, args);
    380                 if (autoFlush)
    381                     out.flush();
    382             }
    383         } catch (InterruptedIOException x) {
    384             Thread.currentThread().interrupt();
    385         } catch (IOException x) {
    386             trouble = true;
    387         }
    388         return this;
    389     }
    390 
    391     // 根据“Locale值(区域属性)”来格式化数据
    392     public PrintWriter format(Locale l, String format, Object ... args) {
    393         try {
    394             synchronized (lock) {
    395                 ensureOpen();
    396                 if ((formatter == null) || (formatter.locale() != l))
    397                     formatter = new Formatter(this, l);
    398                 formatter.format(l, format, args);
    399                 if (autoFlush)
    400                     out.flush();
    401             }
    402         } catch (InterruptedIOException x) {
    403             Thread.currentThread().interrupt();
    404         } catch (IOException x) {
    405             trouble = true;
    406         }
    407         return this;
    408     }
    409 
    410     // 将“字符序列的全部字符”追加到“PrintWriter输出流中”
    411     public PrintWriter append(CharSequence csq) {
    412         if (csq == null)
    413             write("null");
    414         else
    415             write(csq.toString());
    416         return this;
    417     }
    418 
    419     // 将“字符序列从start(包括)到end(不包括)的全部字符”追加到“PrintWriter输出流中”
    420     public PrintWriter append(CharSequence csq, int start, int end) {
    421         CharSequence cs = (csq == null ? "null" : csq);
    422         write(cs.subSequence(start, end).toString());
    423         return this;
    424     }
    425 
    426     // 将“字符c”追加到“PrintWriter输出流中”
    427     public PrintWriter append(char c) {
    428         write(c);
    429         return this;
    430     }
    431 }
    复制代码

    示例代码

    关于PrintWriter中API的详细用法,参考示例代码(PrintWriterTest.java):

    复制代码
      1 import java.io.PrintWriter;
      2 import java.io.File;
      3 import java.io.FileOutputStream;
      4 import java.io.IOException;
      5 
      6 /**
      7  * PrintWriter 的示例程序
      8  *
      9  * @author skywang
     10  */
     11 public class PrintWriterTest {
     12 
     13     public static void main(String[] args) {
     14 
     15         // 下面3个函数的作用都是一样:都是将字母“abcde”写入到文件“file.txt”中。
     16         // 任选一个执行即可!
     17         testPrintWriterConstrutor1() ;
     18         //testPrintWriterConstrutor2() ;
     19         //testPrintWriterConstrutor3() ;
     20 
     21         // 测试write(), print(), println(), printf()等接口。
     22         testPrintWriterAPIS() ;
     23     }
     24 
     25     /**
     26      * PrintWriter(OutputStream out) 的测试函数
     27      *
     28      * 函数的作用,就是将字母“abcde”写入到文件“file.txt”中
     29      */
     30     private static void testPrintWriterConstrutor1() {
     31         final char[] arr={'a', 'b', 'c', 'd', 'e' };
     32         try {
     33             // 创建文件“file.txt”的File对象
     34             File file = new File("file.txt");
     35             // 创建文件对应FileOutputStream
     36             PrintWriter out = new PrintWriter(
     37                     new FileOutputStream(file));
     38             // 将“字节数组arr”全部写入到输出流中
     39             out.write(arr);
     40             // 关闭输出流
     41             out.close();
     42         } catch (IOException e) {
     43             e.printStackTrace();
     44         }
     45     }
     46 
     47     /**
     48      * PrintWriter(File file) 的测试函数
     49      *
     50      * 函数的作用,就是将字母“abcde”写入到文件“file.txt”中
     51      */
     52     private static void testPrintWriterConstrutor2() {
     53         final char[] arr={'a', 'b', 'c', 'd', 'e' };
     54         try {
     55             File file = new File("file.txt");
     56             PrintWriter out = new PrintWriter(file);
     57             out.write(arr);
     58             out.close();
     59         } catch (IOException e) {
     60             e.printStackTrace();
     61         }
     62     }
     63 
     64     /**
     65      * PrintWriter(String fileName) 的测试函数
     66      *
     67      * 函数的作用,就是将字母“abcde”写入到文件“file.txt”中
     68      */
     69     private static void testPrintWriterConstrutor3() {
     70         final char[] arr={'a', 'b', 'c', 'd', 'e' };
     71         try {
     72             PrintWriter out = new PrintWriter("file.txt");
     73             out.write(arr);
     74             out.close();
     75         } catch (IOException e) {
     76             e.printStackTrace();
     77         }
     78     }
     79 
     80     /**
     81      * 测试write(), print(), println(), printf()等接口。
     82      */
     83     private static void testPrintWriterAPIS() {
     84         final char[] arr={'a', 'b', 'c', 'd', 'e' };
     85         try {
     86             // 创建文件对应FileOutputStream
     87             PrintWriter out = new PrintWriter("other.txt");
     88 
     89             // 将字符串“hello PrintWriter”+回车符,写入到输出流中
     90             out.println("hello PrintWriter");
     91             // 将0x41写入到输出流中
     92             // 0x41对应ASCII码的字母'A',也就是写入字符'A'
     93             out.write(0x41);
     94             // 将字符串"65"写入到输出流中。
     95             // out.print(0x41); 等价于 out.write(String.valueOf(0x41));
     96             out.print(0x41);
     97             // 将字符'B'追加到输出流中
     98             out.append('B').append("CDEF");
     99 
    100             // 将"CDE is 5" + 回车  写入到输出流中
    101             String str = "GHI";
    102             int num = 5;
    103             out.printf("%s is %d
    ", str, num);
    104 
    105             out.close();
    106         } catch (IOException e) {
    107             e.printStackTrace();
    108         }
    109     }
    110 }
    复制代码

    运行上面的代码,会在源码所在目录生成两个文件“file.txt”和“other.txt”。
    file.txt的内容如下:
    abcde
    other.txt的内容如下:
    hello PrintWriter
    A65BCDEFGHI is 5

  • 相关阅读:
    C # 编码 习惯[转载]
    SilverLight 学习笔记 (一) 画刷:Brush
    55条SEO必备技巧
    c# 不安全代码之指针
    VMare 硬盘扩展和压缩、网络配置
    Win7 防护墙设置——Ping命令,Http访问
    Vs2010 sp1补丁太让人纠结
    不可小瞧的GetHashCode函数
    .NET中颜色的转换方法总结
    VS2010 如何制作项目模板
  • 原文地址:https://www.cnblogs.com/shangxiaofei/p/3844071.html
Copyright © 2011-2022 走看看