System.err与System.out区别(IO操作)

本文介绍如何使用Java的PrintStream类来重定向程序的标准输出(System.out)到文件,并提供了具体的代码示例。此外还介绍了如何恢复默认输出以及PrintStream类的一些特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;

public class B {
   
public static void main(String[] args) {
     
try {
       System.setOut(
new PrintStream(new FileOutputStream(new File("c:/test.txt"))));
       System.out.println(
"haha");
     }
 catch (Exception e) {
       e.printStackTrace();
     }

   }

}
 


*******************************************************************************************************************************


System.err和System.out 就是错误输出和标准输出
如果你用LOG4J记录日志的话,且设定错误等级的话
System.err的输出是将记录到日志中

*******************************************************************************************************************************
在你setout之前,把System.out赋值给一个PrintStream对象;如:   
   PrintStream    
new    =    new    PrintStream(....);   
   PrintStream    old    
=    System.out;   
   System.setout(
new);   
   ......   
   .....   
   System.setout(old);
//这样就恢复到默认输出状态了.

******************************************************************************************************************************* 
一个PrintStream 可以被创建为自动刷新的;这意味着当一个字节数组(bytearray)被写入,或者某个println 方法被调用,或者一个换行字符或字节(‘ ‘)被写入之后,PrintStream 类型的flush 方法就会被自动地调用。
//第一种 System.out.write(b); //第二种要flush for(int i = 0; i <greetin...

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值