打印字节流和字符流
printStream/ printWriter的构造器和方法都是一样的
package printfile;import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.nio.charset.Charset;public class PrintTest {//目标:掌握打印流 printStream/ printWriterpublic static void main(String[] args) {try (// 创建打印管道//打印字节// PrintStream ps=new PrintStream("src/zFIle/StreamPrint.txt", ("GBK"));// PrintStream ps=new PrintStream("src/zFIle/StreamPrint.txt", Charset.forName("GBK"));//PrintStream ps=new PrintStream("src/zFIle/StreamPrint.txt", Charset.forName("UTF-8"));//打印字符输出//高级流不能追加数据// PrintWriter ps=new PrintWriter("src/zFIle/StreamPrint.txt", Charset.forName("GBK"));PrintWriter ps= new PrintWriter(new FileOutputStream("src/zFIle/StreamPrint.txt", true));){ps.print("97" );ps.print("'a'" );ps.print("我爱你中国" );ps.print(true );ps.println(67.6 );ps.println("love" );//写字节数据ps.write(97);} catch (Exception e) {e.printStackTrace();}}
}
![](https://img-blog.csdnimg.cn/00fa153d76a242e3a8446a46715b59e2.png)
![](https://img-blog.csdnimg.cn/e60bb18c4a614171b71d0cd42eba1286.png)