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. Oracle关于date类型数据的总结

    往Oracle数据库中插入日期型数据(to_date的用法) INSERT  INTO  FLOOR  VALUES  ( to_date ( '2007-12-20 18:31:34' , 'YYY ...

  2. Linux关闭iptables以及selinux

    1.查看iptables状态 /etc/init.d/iptables status # 方法1 service iptables status # 方法2 2.重启后永久生效 chkconfig i ...

  3. Tkinter PanedWindow

         Tkinter PanedWindow: 一个PanedWindow是一个容器部件可能包含任何数量的窗格,水平或垂直排列 一个PanedWindow是一个容器部件可能包含任何数量的窗格,水平 ...

  4. Python实践练习:多重剪贴板

    题目 假定你有一个无聊的任务,要填充一个网页或软件中的许多表格,其中包含一些文本字段.剪贴板让你不必一次又一次输入同样的文本,但剪贴板上一次只有一个内容.如果你有几段不同的文本需要拷贝粘贴,就不得不一 ...

  5. **解释器全局锁(Global Interpreter Lock)

    解释器全局锁(Global Interpreter Lock),即Python为了保证线程安全而采取的独立线程运行的限制,说白了就是一个核只能在同一时间运行一个线程. [解决办法就是多进程和协程(协程 ...

  6. mediawiki的安装

    1. yum install httpd php pcre php-mysql php-pear php-pecl-apc mysql-server ImageMagick sendmail php- ...

  7. UNITY 内存问题资料收集

    1,https://blog.csdn.net/wetest_tencent/article/details/52130703 2,http://blog.51cto.com/13638120/208 ...

  8. jira 安装

    jira jira是Atlassian公司出品的项目与事务跟踪工具,被广泛应用于缺陷跟踪(bug管理).客户服务.需求收集.流程审批.任务跟踪.项目跟踪和敏捷管理等工作领域.同禅道等类似. 安装前准备 ...

  9. 安装mongodb时报错 configure: error: Cannot find OpenSSL's libraries

    请安装这些包以便继续: apt-get install build-essential libexpat1-dev libgeoip-dev libpng-dev libpcre3-dev libss ...

  10. [KVM][guestfs] 安装 guestfs-python 出错

    pip install http://download.libguestfs.org/python/guestfs-1.36.13.tar.gz 执行后出错: 然后百度.谷歌,都是说安装 gcc 或者 ...