Java--IO--PrintStream和PrintWriter打印流
- PrintStream和PrintWriter打印流介绍:
-
PrintWriter和PrintStream打印流的使用
-
PrintStream:
-
package com.model.io.outputstream.printstream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/6/19 12:06 */ public class PrintStreamDemo01 { public static void main(String[] args) throws IOException { //打印流指定打印的位置 String filePath="D:\\qq\\IDEA\\IdeaProjects\\java_mianshi_test\\mianshi_io\\src\\main\\resources\\File\\PrintStreamDemo01.txt"; //创建一个打印流对象 PrintStream printStream = new PrintStream(filePath); //执行打印 printStream.println("aaaaaa"); printStream.close(); PrintStream printStream1=System.out; //System.in的底层就是一个PrintStream打印流对象,且打印的位置是控制台 printStream1.write("张紫韩".getBytes()); printStream1.write("张紫韩".getBytes()); printStream1.close(); //为System.out指定打印位置:改变打印的位置: System.setOut(new PrintStream(filePath)); System.out.println("张紫韩"); } }
-
-
PrintWriter的使用:
-
package com.model.io.writer.printwriter; import java.io.*; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/6/19 13:43 * * 演示:PrintWriter字符打印流的使用 */ public class PrintWriterDemo01 { public static void main(String[] args) throws IOException { String filePath="D:\\qq\\IDEA\\IdeaProjects\\java_mianshi_test\\mianshi_io\\src\\main\\resources\\File\\PrintWriterDemo01.txt"; PrintWriter printWriter = new PrintWriter(new FileWriter(filePath,true)); printWriter.write("张紫韩"); printWriter.close(); PrintWriter printWriter1 = new PrintWriter(System.out); printWriter1.write("张紫韩////"); printWriter1.close(); System.setOut(new PrintStream(filePath)); System.out.println("张紫韩0000"); } }
-
-