打印流 PrintStream and PrintWriter

来源:互联网 发布:摩亨佐达罗 知乎 编辑:程序博客网 时间:2024/05/17 22:14


public class PrintDemo {

/**
 * 打印流: 打印流可以把各种数据类型都原样打印 
 * 字节打印流: 
 * PrintStrream: 构造函数可以接受的对象类型:
 *  1,File对象;File
 *  2,字符串路径;String
 *  3,字节输出流;OutputStream
 *  
 * 字符打印流: PrintWriter:
 * 1,File对象;File
 * 2,字符串路径;String 
 * 3,字节输出流;OutputStream 
 * 4,字符输出流;Writer
 * 
 * 
 * @throws IOException
 */

public static void main(String[] args) throws IOException {
/*
 * //PrintStream 字节打印流的打印方法 //从键盘输入 System.out.println("请输入数据:");
 * BufferedReader buffr= new BufferedReader(new
 * InputStreamReader(System.in)); //用StreamPrint 打印 PrintStream print =
 * new PrintStream(System.out); String line=null;
 * while((line=buffr.readLine())!=null){ print.print(line);
 * print.flush();
 * 
 * } print.close(); buffr.close();
 */

// PrintWriter 字符打印流
System.out.println("请输入数据:");
BufferedReader buffr = new BufferedReader(new InputStreamReader(
System.in));
PrintWriter pw = new PrintWriter(new FileWriter("D:\\print.txt"), true);
String line = null;
while ((line = buffr.readLine()) != null) {
pw.println(line);

// pw.flush();
}
buffr.close();
pw.close();
}

}
0 0