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. 第二章 伪分布式安装hadoop hbase

    安装单机模式的hadoop无须配置,在这种方式下,hadoop被认为是一个单独的java进程,这种方式经常用来调试.所以我们讲下伪分布式安装hadoop. 我们继续上一章继续讲解,安装完先试试SSH装 ...

  2. ORA-01146: cannot start online backup - file 1 is already in backup ORA-01110: data file 1: 'C:\ORACLE\ORADATA\ORCL8\SYSTEM01.DBF'

    问题: Error: [1146] ORA-01146: cannot start online backup - file 1 is already in backup ORA-01110: dat ...

  3. docker 学习(十一) 镜像常用命令

    1 创建账户,创建仓库   首先在dockerhub上有自己的账户,然后创建一个repository(如上图), 然后创建一个名字为robinfei/consumer的仓库. 2  本地镜像打标签(比 ...

  4. module解析过程

    加载一个核心模块时 直接require('模块名')即可 加载一个文件模块时 直接require('绝对路径/相对路径')即可,可省略文件后缀.js. 因为如果文件不存在,将试图找文件名.js的文件 ...

  5. c++获取lua嵌套table某属性的值

    开发环境:vs2012 lua版本:LuaJIT-2.0.2 lua文件作为配置文件,c++读取这个配置. lua配置结构如下 SceneConfig = { [] = { name =}, [] = ...

  6. centos 6.3 kickstart 装机卡在 unsupported hardware detected 页面

    起因 最近厂里一批 dell 新机器 centos 6.3 装机卡在 Unsupported Hardware Detected 页面,要人肉点击 OK 才能继续装机: Unsupported Har ...

  7. C投票系统

  8. C命令行参数

    总是忘了,在这里说明下. argc是命令行参数的实际个数,从1开始. 第一个是可执行文件的名称 argv[]的元素是字符串 每个元素是个命令行参数. #include<stdio.h> i ...

  9. fatal error C1010: 在查找预编译头时遇到意外的文件结尾 (转)

    错误描述:fatal error C1010: 在查找预编译头时遇到意外的文件结尾.是否忘记了向源中添加“#include "stdafx.h"”? 错误分析:     此错误发生 ...

  10. python:随机数 random

    #随机数 import random print(random.randint(10,12))#生成10-12之间的整数 print(random.uniform(10,12))#生成10-12之间的 ...