PrintStream打印流

 1 package file;
 2 
 3 import java.io.File;
 4 import java.io.FileOutputStream;
 5 import java.io.IOException;
 6 import java.io.PrintStream;
 7 
 8 /*
 9  打印流:(printStream)将任意数据转换成字符串在打印。
10  也能打印对象,和收集日志信息
11  
12  
13  */
14 
15 public class Demo13 {
16     public static void main(String[] args) throws IOException {
17         printTest();
18     }
19     
20     public static void printTest() throws IOException {
21         File file = new File("F:\a.txt");
22         PrintStream printStream = new PrintStream(file);
23 /*        printStream.println("你好啊");
24         printStream.println(97);
25         printStream.println(3.14);
26         printStream.println('b');
27         printStream.println(true);
28 */        
29         
30         //默认标准的输出流就是输向控制台
31         System.setOut(printStream);    //重新设置了标准的输出流对象
32         System.out.println("重新设置了输出地");
33         
34         //收集异常的日志信息:
35         File logFile = new File("F:\2000.log");
36         //追加信息:
37         PrintStream logPrintStream = new PrintStream(new FileOutputStream(logFile,true));
38         try{
39             int a = 1/0;
40             System.out.println("a="+a);
41         }
42         catch (Exception e) {
43             e.printStackTrace(logPrintStream);
44         }
45         //关闭资源
46         printStream.close();
47     }
48     
49 }
原文地址:https://www.cnblogs.com/linst/p/5667000.html