PrintWriter是一种过滤流,也是一种处理流,即能对字节流和字符流进行处理。

1.查询API后,我们发现,会有八种构造方法。即:

  • PrintWriter(File file)

    Creates a new PrintWriter, without automatic line flushing, with the specified file.
    PrintWriter(File file, String csn)

    Creates a new PrintWriter, without automatic line flushing, with the specified file and charset.
    PrintWriter(OutputStream out)

    Creates a new PrintWriter, without automatic line flushing, from an existing OutputStream.
    PrintWriter(OutputStream out, boolean autoFlush)

    Creates a new PrintWriter from an existing OutputStream.
    PrintWriter(String fileName)

    Creates a new PrintWriter, without automatic line flushing, with the specified file name.
    PrintWriter(String fileName, String csn)

    Creates a new PrintWriter, without automatic line flushing, with the specified file name and charset.
    PrintWriter(Writer out)

    Creates a new PrintWriter, without automatic line flushing.
    PrintWriter(Writer out, boolean autoFlush)

    Creates a new PrintWriter.

2.实现了三种接口。

1) Closeable接口, 所以它有pw.close()方法来实现对PrintWriter的关闭。

2) Flushable接口,所以它有pw.flush()方法来实现人为的刷新。

3) Appendable接口,所以它有pw.append(char c)方法来向此输出流中追加指定字符,等价于print().

3.方法自寻查询api

4.举例:

 import java.io.IOException;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.File; public class PrintWriterDemo { public static void main(String[] args) {
PrintWriter pw = null;
String name = "张松伟";
int age = ;
float score = 32.5f;
char sex = '男';
try {
pw = new PrintWriter(new FileWriter(new File("e:\\file.txt")), true);
pw.printf("姓名:%s;年龄:%d;性别:%c;分数:%5.2f;", name, age, sex, score);
pw.println();
pw.println("多多指教");
pw.write(name.toCharArray());
} catch (IOException e) {
e.printStackTrace();
} finally {
pw.close();
}
}
}

PrintWriter类的更多相关文章

  1. 学习PrintWriter类 .

    java.io包 1)首先先知道它的八种构造方法,但怎么记住这八种呢?我们都知道PrintWriter是一种过滤流,也叫处理流.也就是能对字节流和字符流进行处理,所以它会有: PrintWriter( ...

  2. [Java]借助PrintWriter类和StringWriter类,取出异常堆栈信息放入字符串中

    在程序开发中,有时我们不仅需要将异常堆栈信息打印在控制台里或是log里,可能还需要将它存在String中,再送到合适的地方,如错误页面,数据库等. 要取异常堆栈信息,具体的函数就是: /** * Ge ...

  3. 深入研究java.lang.Runtime类

    一.概述      Runtime类封装了运行时的环境.每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接.      一般不能实例化一个Runtime对象, ...

  4. 浅析Java.lang.Runtime类

    一.概述      Runtime类封装了运行时的环境.每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接.      一般不能实例化一个Runtime对象, ...

  5. 字节流和字符流(PrintStream类和PrintWiter类)

    要想输入和输出各种数据类型,通常要打印输入流PrintStream和PrintWriter.其中,PrintStream操作的是字节,PrintWriter操作的是字符. 1:PrintStream类 ...

  6. 深入研究java.lang.Runtime类【转】

    转自:http://blog.csdn.net/lastsweetop/article/details/3961911 目录(?)[-] javalang 类 Runtime getRuntime e ...

  7. 关于JAVA System常见类的一些总结

    一.JAVA System类概述 1.概述: System 类是一个抽象类,所有的字段和方法都是静态的,即不能被实例化.其中包含一些有用的类字段和方法,它不能被实例化.在 System 类提供的设施中 ...

  8. 【转】深入研究java.lang.Runtime类

    一.概述      Runtime类封装了运行时的环境.每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接.      一般不能实例化一个Runtime对象, ...

  9. PrintWriter用法简析

    public class PrintWriterextends Writer 向文本输出流打印对象的格式化表示形式.此类实现在 PrintStream 中的所有 print 方法.它不包含用于写入原始 ...

随机推荐

  1. jQuery中this与$(this)的区别实例

    <p><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "<a hre ...

  2. node的express中使用socket.io

    服务器端server.js代码 var express=require("express"); var http=require("http"); var si ...

  3. cinder backup ceph的配置和使用

    Backup 是将 volume 备份到别的地方(备份设备),将来可以通过 restore 操作恢复. 初看 backup 功能好像与 snapshot 很相似,都可以保存 volume 的当前状态, ...

  4. Tkinter Cursors

      Tkinter Cursors:   Python的Tkinter的支持很多不同的鼠标光标的数字.确切的图形可能会有所不同,根据您的操作系统. 这里是大量有趣的的名单: "arrow&q ...

  5. 跟着太白老师学python 10day 函数的动态参数 *args, **kwargs, 形参的位置顺序

    1. *args 接收实参的位置参数, **kwargs接收实参的关键字参数 def func(*args, **kwargs): print(args, kwargs) func(1, 2, 3, ...

  6. C#基础编程试题 4

    一.题目 使用递归算法和二维数组,根据下面条件排序上面的对象. 五个人 张三 李四 王五 猴六 麻七 五间房子 黄房子 蓝房子 红房子 绿房子 橙房子 五只宠物 蜗牛 小狗 小猫 小白兔 小金鱼 五个 ...

  7. effective javascript 学习心得

    第2条:理解JavaScript的浮点数 1.js数字只有一种类型 2.见代码 /** * Created by Administrator on 2017/7/2. */ console.log(& ...

  8. Elasticsearch 搜索引擎

    简介: Elasticsearch 是一个实时的分布式搜索和分析引擎.它可以帮助你用前所未有的速度去处理大规模数据.它可以用于全文搜索,结构化搜索以及分析.    分布式实时文件存储,并将每一个字段都 ...

  9. 运维自动化工具 Cobbler

    简介: 关于操作系统安装方面的自动化,早前我们使用 RedHat 推出的 Kickstart 来批量安装操作系统,近年来 RedHat 又推出一个 Cobbler . Cobbler 使用 Pytho ...

  10. Dubbo限制大数据传输的解决方案

    当service层向web层传输大数据容量的对象时,会受到Dubbo的限制,报类似如下异常: com.alibaba.dubbo.remoting.transport.AbstractCodec.ch ...