博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PrintWriter类
阅读量:4521 次
发布时间:2019-06-08

本文共 1802 字,大约阅读时间需要 6 分钟。

PrintWriter是一种过滤流,也是一种处理流,即能对字节流和字符流进行处理。

1.查询API后,我们发现,会有八种构造方法。即:

  • ( file)
    Creates a new PrintWriter, without automatic line flushing, with the specified file.
    ( file,  csn)
    Creates a new PrintWriter, without automatic line flushing, with the specified file and charset.
    ( out)
    Creates a new PrintWriter, without automatic line flushing, from an existing OutputStream.
    ( out, boolean autoFlush)
    Creates a new PrintWriter from an existing OutputStream.
    ( fileName)
    Creates a new PrintWriter, without automatic line flushing, with the specified file name.
    ( fileName,  csn)
    Creates a new PrintWriter, without automatic line flushing, with the specified file name and charset.
    ( out)
    Creates a new PrintWriter, without automatic line flushing.
    ( 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.举例:

1 import java.io.IOException; 2 import java.io.PrintWriter; 3 import java.io.FileWriter; 4 import java.io.File; 5  6 public class PrintWriterDemo { 7  8     public static void main(String[] args) { 9         PrintWriter pw = null;10         String name = "张松伟";11         int age = 22;12         float score = 32.5f;13         char sex = '男';14         try {15             pw = new PrintWriter(new FileWriter(new File("e:\\file.txt")), true);16             pw.printf("姓名:%s;年龄:%d;性别:%c;分数:%5.2f;", name, age, sex, score);17             pw.println();18             pw.println("多多指教");19             pw.write(name.toCharArray());20         } catch (IOException e) {21             e.printStackTrace();22         } finally {23             pw.close();24         }25     }26 }

 

转载于:https://www.cnblogs.com/laurdawn/p/5644195.html

你可能感兴趣的文章
java基础43 IO流技术(输入字节流/缓冲输入字节流)
查看>>
计算一个整数二进制中1的个数
查看>>
netdom join 错误:指定的域不存在,或无法联系。
查看>>
Android中Dialog的使用
查看>>
Android Activity接收Service发送的广播
查看>>
[Leetcode] Spiral Matrix | 把一个2D matrix用螺旋方式打印
查看>>
加速和监控国际网络
查看>>
【Flex】读取本地XML,然后XML数据转成JSON数据
查看>>
字符串循环右移-c语言
查看>>
解决从pl/sql查看oracle的number(19)类型数据为科学计数法的有关问题
查看>>
古训《增广贤文》
查看>>
职场的真相——七句话
查看>>
xcode命令行编译时:codesign命令,抛出“User interaction is not allowed.”异常 的处理...
查看>>
[转载]开机出现A disk read error occurred错误
查看>>
STM32 C++编程 002 GPIO类
查看>>
无线冲方案 MCU vs SoC
查看>>
进程装载过程分析(execve系统调用分析)
查看>>
在windows 7中禁用media sense
查看>>
ELK-Elasticsearch安装
查看>>
Android 模拟器(Emulator)访问模拟器所在主机
查看>>