在java.io包中提供有两个打印流的处理类:PrintStream(字节打印流)、PrintWriter(字符打印流)。

首先来观察PrintStream类的继承结构与构造方法:
。public class PrintStream extends FilterOutputStream implements Appendable, Closeable
。public PrintStream(OutputStream out),需要通过外部设置输出位置

 1 package cn.demo;
 2 
 3 import java.io.File;
 4 import java.io.FileOutputStream;
 5 import java.io.PrintStream;
 6 
 7 public class Test {
 8     public static void main(String[] args) throws Exception {
 9         PrintStream pu = new PrintStream(new FileOutputStream(new File("F:" + File.separator +"hello.txt")));
10         pu.print("姓名:");
11         pu.println("洋哥");
12         pu.print("年龄:");
13         pu.println(18);
14         pu.close();
15     }    
16 }

结果:

姓名:洋哥
年龄:18

总结:以后要通过程序输出一些文字信息的时候,就使用打印流。

相关文章:

  • 2022-12-23
  • 2021-11-09
  • 2020-05-13
  • 2021-08-26
  • 2022-12-23
  • 2021-06-14
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-08
  • 2022-12-23
  • 2021-08-26
  • 2022-12-23
相关资源
相似解决方案