Java IO (16) -- PrintStream 类

1. 概念

PrintStream 继承了 FilterOutputStream,是"装饰类"的一种,所以属于字节流体系中为其他的输出流添加功能,使它们能够方便打印各种数据值的表示形式。此外,值得注意的是:

  1. 与其他流不同的是,PrintStream 流永远不会抛出异常,因为做了 try{}catch(){} 会将异常捕获,出现异常情况会在内部设置标识,通过 checkError() 获取此标识
  2. PrintStream 流有自动刷新机制,例如当向 PrintStream 流中写入一个字节数组后自动调用 flush() 方法

PrintStream 流打印的字符通过平台默认的编码方式转换成字节,在写入的是字符而不是字节的情况下,应该使用 PrintWriter。PrintStream 流中基本所有的 print(Object obj) 重载方法和 println(Object obj) 重载方法都是通过将对应数据先转换成字符串,然后调用 write() 方法写到底层输出流中。常见用到 PrintStream 流:System.out 就被包装成 PrintStream 流,System.err 也是 PrintStream 流,注意,System.in 不是 PrintStream,而是没有包装过的 OutputStream。所以 System.in 不能直接使用。

PrintStream 流不是直接将数据写到文件的流,需要传入底层输出流 out,而且要实现指定编码方式,需要中间流 OutputStreamWriter。OutputStreamWriter 流实现了字符流以指定编码方式转换成字节流。此外为了提高写入文件的效率,使用到了字符缓冲流 BufferWriter。

写入 PrintStream 流的数据怎么写到文件中?需要先了解一下数据读取和写入的流程:

  1. 数据从流写到文件过程:输出流----->缓冲流----->转化流----->文件流------>文件
  2. 数据从文件到流的过程:文件----->文件流----->转化流----->缓冲流----->输入流

所以从 PrintStream 流写到文件的过程是:
在这里插入图片描述

2. 字段

  1. private final boolean autoFlush:是否自动刷新缓冲区
  2. private boolean trouble = false:是否抛出异常的内部标识.当PrintStream流内部抛出异常时会捕获异常,然后将trouble的值设置成true
  3. private Formatter formatter:用于数据格式化的对象Formatter
  4. private BufferedWriter textOut:PrintStream流本身不具备指定编码功能,BufferedWriter提供了缓冲数据的功能
  5. private OutputStreamWriter charOut:而OutputStreamWriter提供了按照指定编码方法将字符转化成字节的功能

3. 方法

1. 构造器

  1. public PrintStream(OutputStream out):创建了默认编码方式的PrintStream流,字节输出流out作为PrintStream流的输出流,不自动刷新
  2. public PrintStream(OutputStream out, boolean autoFlush):创建默认编码方式的PrintStream流,字节输出流out作为PrintStream流的输出流,传入是否自动刷新的参数autoFlush
  3. public PrintStream(OutputStream out, boolean autoFlush, String encoding):创建了指定编码名称encoding的PrintStream,字节输出流out作为PrintStream流的输出流.传入是否自动刷新的参数autoFlush
  4. public PrintStream(String fileName):创建了指定文件名称,默认字符编码方式的PrintStream流,FileOutputStream流作为PrintStream流的输出流.不自动刷新
  5. public PrintStream(String fileName, String csn):创建指定了文件名称和字符编码名称csn的PrintStream流,FileOutputStream作为PrintStream流的输出流.不自动刷新
  6. public PrintStream(File file):创建指定文件对象File和默认编码方式的PrintStream流,FileOutputStream作为PrintStream流的输出流.不自动刷新
  7. public PrintStream(File file, String csn):创建指定文件对象File和编码名称csn的PrintStream流,FileOutputStream作为PrintStream流的输出流.不自动刷新

2. 其他方法

  1. public void flush():刷新流,将缓冲的数据写到底层输出流中
  2. public void close():关闭流,释放关联的资源
  3. public boolean checkError():检查流中异常状态,如果PrintStream流中有异常抛出,返回true
  4. public void write(int b):将单个字节b写到PrintStream流中
  5. public void write(byte buf[], int off, int len):将字节数组buf中off位置开始,len个字节写到PrintStream流中
  6. public PrintStream printf(String format, Object … args):将数据args按照默认的Locale值和format格式进行格式化后写到PrintStream流中,方法执行等同于out.format(format, args)
  7. public PrintStream printf(Locale l, String format, Object … args):将数据args根据Locale值和format格式进行格式化后写到PrintStream输出流中,方法执行等同于out.printf(l, format,args)
  8. public PrintStream format(String format, Object … args):根据默认的Locale值和format格式来格式化数据args
  9. public PrintStream format(Locale l, String format, Object … args):将数据args根据Locale值和format格式进行格式化
  10. public PrintStream append(CharSequence csq, int start, int end):将字符序列csq中start(包含)位置到end(不包含)之间的子字符序列添加到PrintStream输出流中,此方法执行等同于out.print(csq.subSequence(start, end).toString())
  11. public PrintStream append(char c):将单个字符添加到PrintStream输出流中.此方法执行等同于out.print©
  12. public PrintStream append(CharSequence csq):

对于 print(Object obj) 的重载方法与 println(Object obj) 的重载方法总结如下:两个区别是 println(Object obj) 在写完数据后,会写入一个换行符。而这两类方法写入数据时都会先将数据转成字符串,然后调用底层输出流写到文件中(比如boolean类型的数据true,会先转成字符串"true"),所以实际调用的方法是 write(String s)

  1. public void print(boolean b):将boolean类型数据对应字符串写到PrintStream流中
  2. public void print(char c):将char类型数据对应字符串写到PrintStream流中
  3. public void print(int i):将int类型数据对应字符串写到PrintStream流中
  4. public void print(long l):将long类型数据对应字符串写到PrintStream流中
  5. public void print(float f):将float类型数据对应字符串写到PrintStream流中
  6. public void print(double d):将double类型数据对应字符串写到PrintStream流中
  7. public void print(char s[]):将字符数组写到PrintStream流中
  8. public void print(String s):将字符串s写到PrintStream流中
  9. public void print(Object obj):将对象Obj对应字符串写到PrintStream流中
  10. public void println():将换行符写到PrintStream流中
  11. public void println(boolean x)
  12. public void println(char x)
  13. public void println(int x)
  14. public void println(long x)
  15. public void println(float x)
  16. public void println(double x)
  17. public void println(char x[])
  18. public void println(String x)
  19. public void println(Object x)

4. 案例

public class PrintStreamDemo {
  public static void main(String[] args) throws IOException {
    final String fileName = "D:\\java.txt";
    File file = new File(fileName);
    testPrintMethod(fileName, file);
    testOtherMethod(fileName,file);
  }
  
  private static void testOtherMethod(String fileName,File file) throws IOException {
    PrintStream ps = new PrintStream(fileName);
    ps.write("helloworld".getBytes());
    ps.println();
    ps.format("文件名称:%s", file.getName());
    ps.println();
    ps.write(0x41);
    ps.append("abcde");
    ps.close();
    
  }
  
  private static void testPrintMethod(final String fileName, File file) throws FileNotFoundException {
    PrintStream ps = new PrintStream(new FileOutputStream(fileName));
    ps.println('a');
    ps.println("hello");
    ps.println(2345);
    ps.print(3.1415);
    ps.println();//写入换行符.
    ps.printf("文件名称:%s,是否可读:%s", file.getName(),file.canRead());
    ps.println();
    ps.close();
  }
}

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值