1.PrintStream活动地址:CSDN21天学习挑战赛
实例
package com; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintStream; public class PrintWriter_ { public static void main(String[] args) throws IOException { PrintStream out = System.out; //在默认情况下,PrintStream 输出数据的位置是 标准输出,即显示器 out.print("john, hello"); //因为print底层使用的是write , 所以我们可以直接调用write进行打印/输出 out.write("Demo龙,你好".getBytes()); out.close(); //我们可以去修改打印流输出的位置/设备 //1. 输出修改成到 "e:\f1.txt" //2. "hello, 韩顺平教育~" 就会输出到 e:f1.txt //3. public static void setOut(PrintStream out) { // checkIO(); // setOut0(out); // native 方法,修改了out // } System.setOut(new PrintStream("e:\f1.txt")); System.out.println("hello, Demo龙龙龙~"); } }
2.PrintWriter演示结果
package com; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; public class PrintStream_ { public static void main(String[] args) throws IOException { //PrintWriter printWriter = new PrintWriter(System.out); PrintWriter printWriter = new PrintWriter(new FileWriter("e:\f2.txt")); printWriter.print("hi, Demo龙你好~~~~"); printWriter.close();//flush + 关闭流, 才会将数据写入到文件.. } }
演示结果