IO流_PrintWriter(字符打印流)与PrintStream(字节打印流)
PrintStream:
1.提供了打印方法可以对多种数据类型值进行打印,并保持数据的表示形式
2.它不抛IOException
3.构造函数接受三种类型的值:
字符串路径
File对象
字节输出流
import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintStream; public class PrintStreamDemo { public static void main(String[] args) throws IOException { PrintDemo(); } private static void PrintDemo() throws IOException { PrintStream out = new PrintStream("F:\\print1.txt"); //out.write(97);//写入门的是一个字节,只写最低8位,就是最低的那个字节 out.print(97);//写入的是原样的数据,原理是先将数据变成字符串保持原样将数据打印到目的地 out.close(); } }
PrintWriter:
构造函数参数:
字符串路径
File对象
字节输出流
字符输出流
import java.io.BufferedReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; public class PrintWriterDemo { public static void main(String[] args) throws IOException { PrintDemo(); } public static void PrintDemo() throws IOException { BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in)); //PrintWriter out = new PrintWriter(System.out,true); PrintWriter out = new PrintWriter(new FileWriter("F:\\0.txt"),true); String line = null; while((line = bufr.readLine())!=null){ if("over".equals(line)){ break; } out.println(line);//PrintWriter out = new PrintWriter(System.out,true);写了true会自动刷新 //out.flush(); } bufr.close(); out.close(); } }