• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Java > Java中的PrintWriter 介绍_动力节点Java学院整理

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

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了java printwriter,java.io.printwriter,java中printwriter,java printwriter用法,java print等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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 源码
 
  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 class PrintWriter extends Writer {
   protected Writer out;
   // 自动flush
   // 所谓“自动flush”,就是每次执行print(), println(), write()函数,都会调用flush()函数;
   // 而“不自动flush”,则需要我们手动调用flush()接口。
   private final boolean autoFlush;
   // PrintWriter是否右产生异常。当PrintWriter有异常产生时,会被本身捕获,并设置trouble为true
   private boolean trouble = false;
   // 用于格式化的对象
   private Formatter formatter;
   private PrintStream psOut = null;
   // 行分割符
   private final String lineSeparator;
   // 获取csn(字符集名字)对应的Chaset
   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);
     }
   }
   // 将“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 (out instanceof java.io.PrintStream) {
       psOut = (PrintStream) out;
     }
   }
   // 创建fileName对应的OutputStreamWriter,进而创建BufferedWriter对象;然后将该BufferedWriter作为PrintWriter的输出流,不自动flush,采用默认字符集。
   public PrintWriter(String fileName) throws FileNotFoundException {
     this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))),
        false);
   }
   // 创建fileName对应的OutputStreamWriter,进而创建BufferedWriter对象;然后将该BufferedWriter作为PrintWriter的输出流,不自动flush,采用字符集charset。
   private PrintWriter(Charset charset, File file)
     throws FileNotFoundException
   {
     this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset)),
        false);
   }
   // 创建fileName对应的OutputStreamWriter,进而创建BufferedWriter对象;然后将该BufferedWriter作为PrintWriter的输出流,不自动flush,采用csn字符集。
   public PrintWriter(String fileName, String csn)
     throws FileNotFoundException, UnsupportedEncodingException
   {
     this(toCharset(csn), new File(fileName));
   }
   // 创建file对应的OutputStreamWriter,进而创建BufferedWriter对象;然后将该BufferedWriter作为PrintWriter的输出流,不自动flush,采用默认字符集。
   public PrintWriter(File file) throws FileNotFoundException {
     this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))),
        false);
   }
   // 创建file对应的OutputStreamWriter,进而创建BufferedWriter对象;然后将该BufferedWriter作为PrintWriter的输出流,不自动flush,采用csn字符集。
   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“PrintWriter输出流中的数据”。
   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;
     }
   }
   // flush“PrintWriter输出流缓冲中的数据”,并检查错误
   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输出流”中。c虽然是int类型,但实际只会写入一个字符
   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, , buf.length);



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • Java中的PrintWriter 介绍_动力节点Java学院整理
  • Java 中的Printstream介绍_动力节点Java学院整理
  • java中 Set与Map排序输出到Writer详解及实例
  • Java中的PrintWriter 介绍_动力节点Java学院整理
  • Java 中的Printstream介绍_动力节点Java学院整理

相关文章

  • 2017-05-28Java设计模式之单例模式详解
  • 2017-05-28java 单例模式和工厂模式实例详解
  • 2017-05-28深入理解Java嵌套类和内部类
  • 2017-05-28spring之Bean的生命周期详解
  • 2017-05-28Java Reference源码解析
  • 2017-05-28J2SE 1.5版本的新特性一览
  • 2017-05-28Java身份证验证方法实例详解
  • 2017-05-28Java环境配置与编译运行详解
  • 2017-05-28Spring Boot 项目发布到 Tomcat 服务器的操作步骤
  • 2017-05-28Java 用反射设置对象的属性值实例详解

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • Java接口的作用_动力节点Java学院整理
    • 详解java实现遍历二叉树的三种情况
    • MySQL+SSM+Ajax上传图片问题
    • Java中的关键字_动力节点Java学院整理
    • Java微信公众平台开发(11) 微信三大平台的关联
    • java三层架构原理与作用小结
    • spring注解识别一个接口的多个实现类方法
    • Java进阶:JNI使用技巧点滴
    • Java中==运算符与equals方法的区别及intern方法详解
    • 详解SpringBoot的事务管理

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有