我们在写java程序的时候, 经常使用System.out .* 来输出相关信息 ;当我们需要将控制台信息输出到本地文件的时候,有很多种方法,比如使用FileOutputStream 类等等;

如果觉得用输出流比较费事或者对于初学者来说,可以用PrintStream 类来输出到文件;

代码如下,很简单方便:

[java] view plain copy
  1. package test;  
  2.   
  3. import java.io.FileNotFoundException;  
  4. import java.io.PrintStream;  
  5.   
  6. public class Out {  
  7.     
  8.     public Out(){    
  9.         try {    
  10.               
  11.             PrintStream print=new PrintStream("E:\\test.txt");  //写好输出位置文件;  
  12.             System.setOut(print);    
  13.         } catch (FileNotFoundException e) {    
  14.             e.printStackTrace();    
  15.         }    
  16.     }  
  17.     public static void main(String [] args)  
  18.     {  
  19.         Out o= new Out();// 构造对象  
  20.         System.out.print("Reallly?");  
  21.         System.out.println("Yes");  
  22.         System.out.println("So easy");  
  23.           
  24.           
  25.     }  
  26.       
  27. }  
输出结果为:

java控制台信息输出到文件(System.out)


相关文章: