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. 第五章 MyEclipse配置hadoop开发环境

    1.首先要下载相应的hadoop版本的插件,我这里就给2个例子: hadoop-1.2.1插件:http://download.csdn.net/download/hanyongan300/62381 ...

  2. linux文件权限,用户和组

    文件权限 默认权限分配 umask umask是通过八进制的数值来定义用户创建文件或目录的默认权限的 安全权限的临界点是,文件默认权限是644,目录默认权限是755 [root@Poppy joker ...

  3. 【UVA】12504 Updating a Dictionary(STL)

    题目 题目     分析 第一次用stringstream,真TMD的好用     代码 #include <bits/stdc++.h> using namespace std; int ...

  4. GroupVarint

    folly/GroupVarint.h folly/GroupVarint.h is an implementation of variable-length encoding for 32- and ...

  5. GIL线程与进程选择

    #!/usr/bin/env python # encoding: utf-8  # Date: 2018/6/18 # 计算密集型.应该启动多进程# from multiprocessing imp ...

  6. 史上最详细的HashMap红黑树解析

      简介:请允许我当一回标题党.好了,言归正传,本篇主要内容便是介绍HashMap的男二号——TreeNode(男一号还是给Node吧,毕竟是TreeNode的爷爷,而且普通节点一般来说也比TreeN ...

  7. [Z] 关于Python Tornado的一些资料

    一个简单的样例: http://osedu.net/article/python/2014-03-18/501.html ioloop的官方doc: http://www.tornadoweb.org ...

  8. GeoServer之sqlserver插件使用

     GeoServer之sqlserver插件使用 安装好sqlserver插件后,点击出现的第一个选项,开始创建wms服务. 输入参数: 工作区:为geoserver中创建的工作区 数据源名称:自定义 ...

  9. Asp .net core api+Entity Framework core 实现数据CRUD数据库中(附Git地址)

    最近在学dotNetCore 所以尝试了一下api 这个功能 不多说了大致实现如下 1.用vs2017建立一个Asp.net  Core Web 应用程序 在弹出的对话框中选择 Web API 项目名 ...

  10. RESTFUL 概念

    1. 什么是REST REST全称是Representational State Transfer,中文意思是表述(编者注:通常译为表征)性状态转移. 它首次出现在2000年Roy Fielding的 ...