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 方法.它不包含用于写入原始 ...
随机推荐
- JPA基本介绍以及使用
JPA即Java Persistence Architecture,Java持久化规范,从EJB2.x版本中原来的实体Bean分离出来的,EJB3.x中不再有实体Bean,而是将实体Bean放到J ...
- node+express+ejs搭建一个简单的"页面"
1.建立工程文件夹my_ejs. 2.首先利用npm install express和npm install ejs 下载这两个家伙.至于要不要设置成全局的,看习惯,我习惯性的下载到本项目中的文件夹中 ...
- 5月8日上课笔记-浮动float
IO文件复制 字符流(只能对文本文件进行操作) Reader Writer 字节流(对所有文件都能操作) InputStream OutputStream 一.浮动 边框弧度 border-radiu ...
- Fiddler监控面板显示Server栏(Fiddler v5.0)
1.点击Rules下的Customize Rules.js,会打开Fiddler ScriptEditor 2.去掉 UI.lvSessions.AddBoundColumn("Server ...
- 使用java获取自己的机器网卡
package org.ibase4j.core.util; import java.io.BufferedReader;import java.io.IOException;import java. ...
- linux find中的-print0和xargs中-0的奥妙
默认情况下, find 每输出一个文件名, 后面都会接着输出一个换行符 ('n'), 因此我们看到的 find 的输出都是一行一行的: 比如我想把所有的 .log 文件删掉, 可以这样配合 xargs ...
- python中nltk的下载安装方式
首先去http://nltk.org/install.html下载相关的安装程序,然后 在cmd窗口中,进入到python的文件夹内的 Scripts内,运行easy_install pip 安装Py ...
- C# Data Parse
一.DateTime 方法一:Convert.ToDateTime(string) string格式有要求,必须是yyyy-MM-dd hh:mm:ss 方法二:Convert.ToDateTime( ...
- delphi BLE 学习
TBluetoothLE 控件 TBluetoothLE.FManager: TBluetoothLEManager; class constructor TBluetoothLEManager.Cr ...
- AMF_OBJECT 数据结构浅析
组织的比较散,主要是标记一下有关 AMF_OBJECT 数据组织结构.其标识嵌套结束则为 0x 00 00 09 原始数据结构已知: key=“0123456”: Value 的值是一个结构体如下: ...