打印流简述
作者:互联网
PrintStream 和 PrintWriter
- 打印流只有输出流,没有输入流
PrintStream
package com.ftn.printStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
public class PrintStream_ {
public static void main(String[] args) throws IOException {
PrintStream out = System.out;
//PrintStream 默认输出位置为显示器
out.println("打印一次");
//因为print底层使用的是write输出,所以可以直接调用write打印
out.write("aaa".getBytes(StandardCharsets.UTF_8));
//可以修改打印流输出的位置/设备
//修改为 d:\m1.txt
System.setOut(new PrintStream("d:\\m1.txt"));
System.out.println("hello,hell");
out.close();
}
}
PrintWriter
package com.ftn.printStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class PrintWriter_ {
public static void main(String[] args) throws IOException {
PrintWriter printWriter = new PrintWriter(System.out);
printWriter.print("hhh");
//更改打印输出位置
PrintWriter printWriter1 = new PrintWriter(new FileWriter("d:\\t1.txt"));
printWriter1.print("hehehe");
printWriter.close(); //必须关闭流之后才能写入数据
printWriter1.close(); //必须关闭流之后才能写入数据
}
}
标签:PrintWriter,java,PrintStream,打印,简述,io,import,out 来源: https://blog.csdn.net/A_JOKER___/article/details/120380272