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");

 

转载于:https://www.cnblogs.com/nhdlb/p/11599500.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值