00098_打印流

1、打印流的概述

  (1)打印流添加输出数据的功能,使它们能够方便地打印各种数据值表示形式;

  (2)打印流根据流的分类:

    ①字节打印流    PrintStream;

    ②字符打印流    PrintWriter;

  (3)方法

    ①void print(String str): 输出任意类型的数据;

    ②void println(String str): 输出任意类型的数据,自动写入换行操作。

  (4)代码演示:

 1 import java.io.IOException;
 2 import java.io.PrintWriter;
 3 
 4 /* 
 5  * 需求:把指定的数据,写入到printFile.txt文件中
 6  * 
 7  * 分析:
 8  *     1,创建流
 9  *     2,写数据
10  *     3,关闭流
11  */
12 public class PrintWriterDemo {
13     public static void main(String[] args) throws IOException {
14         // 创建流
15         // PrintWriter out = new PrintWriter(new FileWriter("printFile.txt"));
16         PrintWriter out = new PrintWriter("d:\Java\printFile.txt");
17         // 2,写数据
18         for (int i = 0; i < 5; i++) {
19             out.println("helloWorld");
20         }
21         // 3,关闭流
22         out.close();
23     }
24 }

2、打印流完成数据自动刷新

  (1)可以通过构造方法,完成文件数据的自动刷新功能;

  (2)构造方法:开启文件自动刷新写入功能。

    ①public PrintWriter(OutputStream out, boolean autoFlush)

    ②public PrintWriter(Writer out, boolean autoFlush)

  (3)代码演示:

 1 import java.io.FileWriter;
 2 import java.io.IOException;
 3 import java.io.PrintWriter;
 4 
 5 /* 
 6  * 分析:
 7  *     1,创建流
 8  *     2,写数据
 9  */
10 public class PrintWriterDemo2 {
11     public static void main(String[] args) throws IOException {
12         // 创建流
13         PrintWriter out = new PrintWriter(new FileWriter(
14                 "d:\Java\printFile.txt"), true);
15         // 2,写数据
16         for (int i = 0; i < 5; i++) {
17             out.println("helloWorld");
18         }
19         // 3,关闭流
20         out.close();
21     }
22 }

1.1      打印流完成数据自动刷新

原文地址:https://www.cnblogs.com/gzdlh/p/8098403.html