PrintWriter类
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类的更多相关文章
- 学习PrintWriter类 .
java.io包 1)首先先知道它的八种构造方法,但怎么记住这八种呢?我们都知道PrintWriter是一种过滤流,也叫处理流.也就是能对字节流和字符流进行处理,所以它会有: PrintWriter( ...
- [Java]借助PrintWriter类和StringWriter类,取出异常堆栈信息放入字符串中
在程序开发中,有时我们不仅需要将异常堆栈信息打印在控制台里或是log里,可能还需要将它存在String中,再送到合适的地方,如错误页面,数据库等. 要取异常堆栈信息,具体的函数就是: /** * Ge ...
- 深入研究java.lang.Runtime类
一.概述 Runtime类封装了运行时的环境.每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接. 一般不能实例化一个Runtime对象, ...
- 浅析Java.lang.Runtime类
一.概述 Runtime类封装了运行时的环境.每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接. 一般不能实例化一个Runtime对象, ...
- 字节流和字符流(PrintStream类和PrintWiter类)
要想输入和输出各种数据类型,通常要打印输入流PrintStream和PrintWriter.其中,PrintStream操作的是字节,PrintWriter操作的是字符. 1:PrintStream类 ...
- 深入研究java.lang.Runtime类【转】
转自:http://blog.csdn.net/lastsweetop/article/details/3961911 目录(?)[-] javalang 类 Runtime getRuntime e ...
- 关于JAVA System常见类的一些总结
一.JAVA System类概述 1.概述: System 类是一个抽象类,所有的字段和方法都是静态的,即不能被实例化.其中包含一些有用的类字段和方法,它不能被实例化.在 System 类提供的设施中 ...
- 【转】深入研究java.lang.Runtime类
一.概述 Runtime类封装了运行时的环境.每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接. 一般不能实例化一个Runtime对象, ...
- PrintWriter用法简析
public class PrintWriterextends Writer 向文本输出流打印对象的格式化表示形式.此类实现在 PrintStream 中的所有 print 方法.它不包含用于写入原始 ...
随机推荐
- Java 基本数据类型最大值极限和最小值极限
想知道 Java 基本数据类型最大值极限和最小值极限,写个小程序就很容易知道. 测试 Integer, Long, Float 和 Double 的最大值和最小值,代码如下: public stati ...
- python学习 (三十一) python中的class
1 python的类: Python类都继承自object. __init__: 构造函数,如果不写,有一个默认的. __init__: 这个构造函数只能有一个,Python中不能有多个构造函数. ...
- HBase快速上手
一.创建单节点HBase实例 https://hbase.apache.org/book.html#quickstart (一)jdk版本要求Java: HBase Version JDK 7 JDK ...
- VMware虚拟机如何设置从U盘启动
要给虚拟机重新安装win7系统,想使用U盘重装系统的方式,就需要让虚拟机从U盘启动,以下内容就是虚拟机从U盘启动的全操作过程. 前期准备: 1.u盘启动盘 2.VMware虚拟机 具体操作步骤: 1. ...
- sql查询语句常用例子
1.查找与jams在同一个单位的员工姓名.性别.部门和职称:select emp_no, emp_name, dept, title from employee where emp_name< ...
- Visual Studio 进行Excel相关开发,Microsoft.Office.Interop.Excel.dll库
1. Interop.Excel.dll 的查找 本文中将 Microsoft.Office.Interop.Excel.dll库简称为Interop.Excel.dll库 其实在使用Visual S ...
- msql修改密码
修改的用户都以root为列.一.拥有原来的myql的root的密码: 方法一:在mysql系统外,使用mysqladmin# mysqladmin -u root -p password " ...
- angularjs 出现 “Possibly unhandled rejection: cancel ”错误
Try adding this to your config. I had a similar is once and this workaround did the trick. app.confi ...
- MySQL数据库篇之索引原理与慢查询优化之一
主要内容: 一.索引的介绍 二.索引的原理 三.索引的数据结构 四.聚集索引与辅助索引 五.MySQL索引管理 六.测试索引 七.正确使用索引 八.联合索引与覆盖索引 九.查询优化神器--explai ...
- 使用heap profiler进行内存占用分析
最近在项目中用到了google的heap profiler工具来分析内存占用,效果非常显著,因此在这里写一篇博客记录一下使用过程中遇到的一些问题. heap profiler依赖于tcmalloc,所 ...