zoukankan      html  css  js  c++  java
  • Java:Java控制台输出保存进文件

    前言

    实现在控制台输出、并且把输出保存进文件

    实现

    您要在两个流中写入数据,请尝试使用OutputStream中的TeeOutputStream对象。

    一、在maven的pom文件中引入jar包。

        <dependency>
          <groupId>org.apache.commons</groupId>
          <artifactId>commons-io</artifactId>
          <version>2.6.0</version>
         </dependency>

    二、手动下载jar包。

    下载路径:http://us.mirrors.quenda.co/apache//commons/io/binaries/commons-io-2.6-bin.zip

    三、实现代码。

    try {
        FileOutputStream fos = new FileOutputStream(f);//f:生成的文件路径
        //we will want to print in standard "System.out" and in "file"
        TeeOutputStream myOut=new TeeOutputStream(System.out, fos);
        PrintStream ps = new PrintStream(myOut, true); //true - auto-flush after println
        System.setOut(ps);
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.print("123456789");
  • 相关阅读:
    BZOJ 1003 物流运输
    549565
    26566
    68
    554554
    5656
    49886
    5989
    6898
    656
  • 原文地址:https://www.cnblogs.com/nhdlb/p/11599500.html
Copyright © 2011-2022 走看看