PrintStream&PrintWriter
显而易见 前者是字节打印流 后者是字符打印流
上代码:
import java.io.IOException;
import java.io.PrintStream;
public class PrintStream_ {
//字节打印流
public static void main(String[] args) throws IOException {
//PrintStream在默认情况下 打印输出的位置是显示器
PrintStream out=System.out;
out.println("打印成功");
//PrintStram的print方法底层是由write实现的 所以也可像下面一样
out.write("abc".getBytes());
out.close();
//打印流也可以更改输出的位置 要求向E盘上的print.txt打印 hello world
String filename="e:/print.txt";
System.setOut(new PrintStream(filename));
System.out.println("hello world");
}
}
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class Printwrite {
public static void main(String[] args) throws IOException {
//PrintWriter的使用
PrintWriter pw=new PrintWriter(System.out);
pw.print(5);
pw.close();
PrintWriter pw1=new PrintWriter(new FileWriter("e:/print.txt",true));
pw1.print("你好世界");
pw1.close();
}
}
注意: PrintWriter如果不close 不会完成输出 而会清空原有数据内容