一.打印流spa
打印流添加輸出數據的功能,使它們可以方便地打印各類數據值表示形式.code
打印流根據流的分類:blog
1.字節打印流 PrintStreamit
2.字符打印流 PrintWriterio
方法:class
void print(String str): 輸出任意類型的數據,方法
void println(String str): 輸出任意類型的數據,自動寫入換行操做數據
/* * 需求:把指定的數據,寫入到printFile.txt文件中 * * 分析: * 1,建立流 * 2,寫數據 * 3,關閉流 */ public class PrintWriterDemo { public static void main(String[] args) throws IOException { //建立流 //PrintWriter out = new PrintWriter(new FileWriter("printFile.txt")); PrintWriter out = new PrintWriter("printFile.txt"); //2,寫數據 for (int i=0; i<5; i++) { out.println("helloWorld"); } //3,關閉流 out.close(); } }
二.打印流完成數據自動刷新static
構造方法:di
開啓文件自動刷新寫入功能
public PrintWriter(OutputStream out, boolean autoFlush)
public PrintWriter(Writer out, boolean autoFlush)
/* * 分析: * 1,建立流 * 2,寫數據 */ public class PrintWriterDemo2 { 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(); } }