• 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 }
  • 相关阅读:
    Sunnypig闯三角关
    送给圣诞夜的贺卡
    uva 1592(NEERC 2009 STL)
    uva 297(传递闭包 WF 1996)
    hdu 4190(二分)
    uva 3592 (MST, kruskal)
    uva 11997 (基础数据结构)
    hdu 2680 (Dijkstra)
    hdu 4568(状态压缩dp)
    hdu 4582 (树上的贪心)
  • 原文地址:https://www.cnblogs.com/linst/p/5667000.html
Copyright © 2020-2023  润新知