多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] # 打印流的概述 平时在控制台打印输出,是调用print方法和println方法完成的,这2个方法来自于java.io.PrintStream类 打印流添加输出数据的功能,使它们能够方便地打印各种数据值表示形式. 打印流根据流的分类: * 字节打印流 PrintStream * 字符打印流 PrintWriter 方法: ~~~ void print(String str): 输出任意类型的数据, void println(String str): 输出任意类型的数据,自动写入换行操作 ~~~ ~~~ public static void main(String[] args) throws IOException { //创建流 //PrintWriter out = new PrintWriter(new FileWriter("printFile.txt")); PrintWriter out = new PrintWriter("printFile.txt"); //写数据 for (int i=0; i<5; i++) { out.println("helloWorld"); } //3,关闭流 out.close(); } ~~~ # 打印流完成数据自动刷新 可以通过构造方法,完成文件数据的自动刷新功能 构造方法: * 开启文件自动刷新写入功能 ~~~ public PrintWriter(OutputStream out, boolean autoFlush) public PrintWriter(Writer out, boolean autoFlush) ~~~ ~~~ public static void main(String[] args) throws IOException { //创建流 PrintWriter out = new PrintWriter(new FileWriter("printFile.txt"), true); //2,写数据 for (int i=0; i<5; i++) { out.println("helloWorld"); } //3,关闭流 out.close(); } ~~~