zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Java IO流之PrintWriter分析「建议收藏」

JAVAIO 分析 建议 收藏 PrintWriter
2023-06-13 09:13:03 时间

大家好,又见面了,我是你们的朋友全栈君。

简介

PrintWriter继承于Writer抽象类,属于字符流的一种,方法包含了写入单个字符和字符数组的方法.但不包含原始字节写入的方法.在设置自动刷新的时候,不像PrintStream流中遇到换行符就会刷新,PrintWriter只有调用了printf,println,format三类方法其中一种才会自动刷新.PrintWriter流永远不会抛出异常,因为当抛出异常的时候,流内部会将异常捕获(try{}catch(){}),然后将内部标识设置成true,表示有异常抛出,调用checkError()方法可获取此标识.

PrintWriter介绍

1.构造方法

1---public PrintWriter (Writer out) {
2---public PrintWriter(Writer out,boolean autoFlush) {}
3---public PrintWriter(OutputStream out) {}
4---public PrintWriter(OutputStream out, boolean autoFlush) {}
5---public PrintWriter(String fileName) {}
6---public PrintWriter(String fileName, String csn){}
7---public PrintWriter(File file){}
8---public PrintWriter(File file, String csn){}
  • 1—-创建默认编码方式的PrintWriter流.字符输出流out作为PrintWriter的输出流,不自动刷新.
  • 2—-创建默认编码方式的PrintWriter流.字符输出流作为PrintWriter的输出流,传入是否自动刷新的参数autoFlush.
  • 3—-创建了默认编码方式的PrintWriter流,字节输出流out作为PrintWriter流的输出流,不自动刷新.
  • 4—-创建默认编码方式的PrintWriter流,字节输出流out作为PrintWiter流的输出流,传入是否自动刷新的参数autoFlush.
  • 5—-创建了指定文件名称,默认字符编码方式的PrintWriter流,FileOutputStream流作为PrintWriter流的输出流.不自动刷新.
  • 6—-创建指定了文件名称和字符编码名称csn的PrintWriter流,FileOutputStream作为PrintWriter流的输出流.不自动刷新.
  • 7—-创建指定文件对象File和默认编码方式的PrintWriter流,FileOutputStream作为PrintWriter流的输出流.不自动刷新.
  • 8—-创建指定文件对象File和编码名称csn的PrintWriter流,FileOutputStream作为PrintWriter流的输出流.不自动刷新.

2.内部变量

protected Writer out;
private final boolean autoFlush;
private boolean trouble = false;
private Formatter formatter;
private PrintStream psOut = null;
private final String lineSeparator;
  • out—-底层字符输出流.
  • autoFlush—-是否自动刷新.
  • trouble—-是否抛出异常的内部标识.当PrintStream流内部抛出异常时会捕获异常,然后将trouble的值设置成true.
  • formatter—-用于数据格式化的对象Formatter.
  • psOut—-字节打印流,用于checkError().
  • lineSeparator—换行符,创建构造方法时会赋值.

3.内部方法

public void flush() {}
public void close() {}
public boolean checkError(){}
public void write(int c) {}
public void write(char buf[], int off, int len) {}
public void write(char buf[]) {}
public void write(String s, int off, int len)
public void write(String s) {}
public PrintWriter printf(String format, Object ... args){}
public PrintWriter printf(Locale l, String format, Object ... args){}
public PrintWriter format(String format, Object ... args){}
public PrintWriter format(Locale l, String format, Object ... args){}
public PrintWriter append(CharSequence csq){}
public PrintWriter append(CharSequence csq, int start, int end){}
public PrintWriter append(char c){}
public void print(boolean b){}
public void print(char c) {}
public void print(int i) {}
public void print(long l) {}
public void print(float f) {}
public void print(double d) {}
public void print(char s[]) {}
public void print(String s) {}
public void print(Object obj) {}
public void println() {}
public void println(boolean x) {}
public void println(char x){}
public void println(int x) {}
public void println(long x) {}
public void println(float x) {}
public void println(double x) {}
public void println(char x[]) {}
public void println(String x) {}
public void println(Object x) {}
  • flush()—-刷新流,将缓冲的数据写到底层输出流中.
  • close()—关闭流,释放关联的资源.
  • checkError()—检查流中异常状态,如果PrintWriter流中有异常抛出,返回true.
  • write(int c)—-将单个字符c写到PrintWriter流中.
  • write(char buf[] ,int off,int len)—-将字符数组buf中off位置开始,len个字节写到PrintWriter流中.
  • write(char buf[])—-将字符数组buf写到PrintWriter流中.
  • printf(String format, Object … args)—-将数据args按照默认的Locale值和format格式进行格式化后写到PrintWriter流中,方法执行等同于out.format(format, args)
  • printf(Locale l, String format, Object … args)—-将数据args根据Locale值和format格式进行格式化后写到PrintWriter输出流中,方法执行等同于out.printf(l, format,args).
  • format(String format, Object … args)—-根据默认的Locale值和format格式来格式化数据args.
  • format(Locale l, String format, Object … args)—-将数据args根据Locale值和format格式进行格式化.
  • append(CharSequence csq, int start, int end)—-将字符序列csq中start(包含)位置到end(不包含)之间的子字符序列添加到PrintWriter输出流中,此方法执行等同于out.print(csq.subSequence(start, end).toString()).
  • append(char c)—-将单个字符添加到PrintWriter输出流中.此方法执行等同于out.print(c).

其他的print(Object obj)的重载方法与println(Object obj)的重载方法总结如下,两个区别是println(Object obj)在写完数据后,会写入一个换行符.而这两类方法写入数据时都会先将数据转成字符串,然后调用底层输出流写到文件中(比如boolean类型的数据true,会先转成字符串”true”).此两类方法都将写入数据转化成了字符串,所以实际调用的方法是write(String s).

修饰符

不写入换行的方法

写入换行的方法(写入数据+换行符)

功能

public

void print(boolean b){}

void println(boolean b){}

将boolean类型数据对应字符串写到PrintWriter流中

public

void print(char c){}

void println(char c){}

将char类型数据对应字符串写到PrintWriter流中

public

void print(int i) {}

void println(int i) {}

将int类型数据对应字符串写到PrintWriter流中

public

void print(long l) {}

void println(long l) {}

将long类型数据对应字符串写到PrintWriter流中

public

void print(float f) {}

void println(float f) {}

将float类型数据对应字符串写到PrintWriter流中

public

void print(double d) {}

void println(double d) {}

将double类型数据对应字符串写到PrintWriter流中

public

void print(char s[]) {}

void println(char s[]) {}

将字符数组写到PrintWriter流中

public

void print(String s) {}

void println(String s) {}

将字符串s写到PrintWriter流中

public

void print(Object obj) {}

void println(Object obj) {}

将对象Obj对应字符串写到PrintWriter流中

public

void println() {}

将换行符写到PrintWriter流中

PrintStream案例

public class PrintWriterDemo {
  public static void main(String[] args) throws IOException {
    final String fileName = "D:\\java.txt";
    File file = new File(fileName);
    testPrintMethod(fileName, file);
    testOtherMethod(fileName,file);
    testOther();
  }
  //构造方法,传入System.out
 private static void testMethod() {
    PrintWriter out = new PrintWriter(System.out);
    String str = "of PrintWriter";
    char buf[] = new char[] {'A','B','D','E','F'};
    out.println(str);
    out.println(buf);
    out.print(true);
    out.print(1);
    out.print(3.14);
    out.println();
    out.print(out);//测试out.print(Object obj);
    out.println();
    out.append('J');
    out.println(out.checkError());
    out.format(Locale.UK,"this is demo %s", str);
    out.flush();
    out.close();
  }  
  
  private static void testOtherMethod(String fileName,File file) throws IOException {
    PrintWriter pw = new PrintWriter(fileName);
    pw.write("helloworld".toCharArray());
    pw.println();
    pw.format("文件名称:%s", file.getName());
    pw.println();
    pw.write(0x41);
    pw.append("abcde");
    pw.close();
    
  }
  
  private static void testPrintMethod(final String fileName, File file) throws FileNotFoundException {
    PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));
    pw.println('a');
    pw.println("hello");
    pw.println(2345);
    pw.print(3.1415);
    pw.println();//写入换行符.
    pw.printf("文件名称:%s,是否可读:%s", file.getName(),file.canRead());
    pw.println();
    pw.close();
  }
}

运行结果:

testPrintMethod()结果:

testOtherMethod()的结果:

testOther()将结果输出到控制台;

of PrintWriter ABDEF true13.14 java.io.PrintWriter@28d93b30 Jfalse this is demo of PrintWriter

PrintStream源码分析

public class PrintWriter extends Writer {

   //底层字符输出流.
    protected Writer out;
    //是否自动刷新缓冲区.
    private final boolean autoFlush;
    //是否抛出异常的内部标识.当PrintWriter流内部抛出异常时会捕获异常,然后将trouble的值设置成true.
    private boolean trouble = false;
    //用于数据格式化的对象Formatter.
    private Formatter formatter;
    //字节打印流,用于checkError()方法
    private PrintStream psOut = null;

    //换行符,构造方法时会赋值.
    private final String lineSeparator;

    //根据字符编码名称返回Chatset对象.
    private static Charset toCharset(String csn)
        throws UnsupportedEncodingException
    {
        Objects.requireNonNull(csn, "charsetName");
        try {
            return Charset.forName(csn);
        } catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
            // UnsupportedEncodingException should be thrown
            throw new UnsupportedEncodingException(csn);
        }
    }

    //创建默认编码方式的PrintWriter流.字符输出流out作为PrintWriter的输出流,不自动刷新.
    public PrintWriter (Writer out) {
        this(out, false);
    }

    //创建默认编码方式的PrintWriter流.字符输出流作为PrintWriter的输出流,传入是否自动刷新的参数autoFlush.
    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"));
    }

    //创建了默认编码方式的PrintWriter流,字节输出流out作为PrintWriter流的输出流,不自动刷新.
    public PrintWriter(OutputStream out) {
        this(out, false);
    }

  //创建默认编码方式的PrintWriter流,字节输出流out作为PrintWiter流的输出流,传入是否自动刷新的参数autoFlush.
    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流,FileOutputStream流作为PrintWriter流的输出流.不自动刷新
    public PrintWriter(String fileName) throws FileNotFoundException {
        this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))),
             false);
    }

    /* Private constructor */
    private PrintWriter(Charset charset, File file)
        throws FileNotFoundException
    {
        this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset)),
             false);
    }

    //创建指定了文件名称和字符编码名称csn的PrintWriter流,FileOutputStream作为PrintWriter流的输出流.不自动刷新
    public PrintWriter(String fileName, String csn)
        throws FileNotFoundException, UnsupportedEncodingException
    {
        this(toCharset(csn), new File(fileName));
    }

    //创建指定文件对象File和默认编码方式的PrintWriter流,FileOutputStream作为PrintWriter流的输出流.不自动刷新
    public PrintWriter(File file) throws FileNotFoundException {
        this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))),
             false);
    }

    //创建指定文件对象File和编码名称csn的PrintWriter流,FileOutputStream作为PrintWriter流的输出流.不自动刷新.
    public PrintWriter(File file, String csn)
        throws FileNotFoundException, UnsupportedEncodingException
    {
        this(toCharset(csn), file);
    }

    //确保流没有关闭.
    private void ensureOpen() throws IOException {
        if (out == null)
            throw new IOException("Stream closed");
    }

    //刷新流,调用flush()会将缓冲数据写到底层输出流中.
    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;
        }
    }

    //刷新流,检查异常状态,如果底层输出流抛出异常,将会返回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;
    }

    //将单个字符c写到PrintWriter流中.
    public void write(int c) {
        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, int len) {
        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(char buf[]) {
        write(buf, 0, buf.length);
    }

    //将字符串s中off位置开始,len个字符写到PrintWriter流中.
    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;
        }
    }
    
    //将字符串s写到PrintWriter流中.
    public void write(String s) {
        write(s, 0, s.length());
    }
  //将换行符写到PrintWriter流中.
    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;
        }
    }

    //将boolean类型数据对应的字符串写到PrintWriter流中,实际调用write()方法.
    public void print(boolean b) {
        write(b ? "true" : "false");
    }

    //将char类型数据对应的字符串写到PrintWriter流中,实际调用write()方法.
    public void print(char c) {
        write(c);
    }

    //将int类型数据对应的字符串写到PrintWriter流中,实际调用write()方法.
    public void print(int i) {
        write(String.valueOf(i));
    }

    //将long类型数据对应的字符串写到PrintWriter流中,实际调用write()方法.
    public void print(long l) {
        write(String.valueOf(l));
    }

    //将float类型数据对应的字符串写到PrintWriter流中,实际调用write()方法.
    public void print(float f) {
        write(String.valueOf(f));
    }

    //将doule类型数据对应的字符串写到PrintWriter流中,实际调用write()方法.
    public void print(double d) {
        write(String.valueOf(d));
    }

    //将字符数组写到PrintWriter流中,实际调用write()方法.
    public void print(char s[]) {
        write(s);
    }

  //将字符串s写到PrintWriter流中,s为null,将会写入"null",实际调用write()方法.
    public void print(String s) {
        if (s == null) {
            s = "null";
        }
        write(s);
    }

    //将对象obj对应的字符串写到PrintWriter流中,实际调用write()方法.
    public void print(Object obj) {
        write(String.valueOf(obj));
    }

    //将换行符写到PrintWriter流中.用于终止当前行(换行符由系统定义)
    public void println() {
        newLine();
    }

    //将boolean类型数据对应字符串+换行符写到PrintWriter流中,实际调用Print()--->write().
    public void println(boolean x) {
        synchronized (lock) {
            print(x);
            println();
        }
    }

    //将char类型数据对应字符串+换行符写到PrintWriter流中,实际调用print()-->write().
    public void println(char x) {
        synchronized (lock) {
            print(x);
            println();
        }
    }

    //将int类型数据对应字符串+换行符写到PrintWriter流中,实际调用print()-->write().
    public void println(int x) {
        synchronized (lock) {
            print(x);
            println();
        }
    }

    //将long类型数据对应字符串+换行符写到PrintWriter流中,实际调用print()-->write().
    public void println(long x) {
        synchronized (lock) {
            print(x);
            println();
        }
    }

    //将float类型数据对应字符串+换行符写到PrintWriter流中,实际调用print()-->write().
    public void println(float x) {
        synchronized (lock) {
            print(x);
            println();
        }
    }

    //将double类型数据对应字符串+换行符写到PrintWriter流中,实际调用print()-->write().
    public void println(double x) {
        synchronized (lock) {
            print(x);
            println();
        }
    }

    //将字符数组+换行符写到PrintWriter流中,实际调用print()-->write().
    public void println(char x[]) {
        synchronized (lock) {
            print(x);
            println();
        }
    }

    //将字符串+换行符写到PrintWriter流中,实际调用print()-->write().
    public void println(String x) {
        synchronized (lock) {
            print(x);
            println();
        }
    }

    //将对象x对应的字符串+换行符写到PrintWriter流中,实际调用print()-->write().
    public void println(Object x) {
        String s = String.valueOf(x);
        synchronized (lock) {
            print(s);
            println();
        }
    }

    //将数据args按照默认的Locale值和format格式进行格式化后写到PrintWriter输出流中.
    //方法执行等同于out.format(format, args)
    public PrintWriter printf(String format, Object ... args) {
        return format(format, args);
    }

    //将数据args根据Locale值和format格式进行格式化后写到PrintWriter输出流中.
    //方法执行等同于out.printf(l, format,args)
    public PrintWriter printf(Locale l, String format, Object ... args) {
        return format(l, format, args);
    }

    //根据默认的Locale值和format格式来格式化数据args.写到PrintWriter输出流中.
    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;
    }

    //将数据args根据Locale值和format格式进行格式化写到PrintWriter输出流中.
    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;
    }

    //将字符序列csq添加到PrintWriter输出流中,此方法执行等同于out.write(csq.toString())
    public PrintWriter append(CharSequence csq) {
        if (csq == null)
            write("null");
        else
            write(csq.toString());
        return this;
    }

    //将字符序列csq中start(包含)位置到end(不包含)之间的子字符序列添加到PrintWriter输出流中
    //此方法执行等同于out.write(csq.subSequence(start, end).toString())
    public PrintWriter append(CharSequence csq, int start, int end) {
        CharSequence cs = (csq == null ? "null" : csq);
        write(cs.subSequence(start, end).toString());
        return this;
    }

    //将单个字符c添加到PrintWriter输出流中.此方法执行等同于out.write(c)
    public PrintWriter append(char c) {
        write(c);
        return this;
    }
}

总结

PrintWriter是字符流的一种.方法有写入单个字符和字符数组的方法,没有对应写入原始字节的方法.与PrintStream有相同的方法print和println方法.

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/169283.html原文链接:https://javaforall.cn