System.out.print重定向到文件实例

来源:互联网 发布:网络门事件 编辑:程序博客网 时间:2024/05/01 00:42

该代码可以实现让System.out.print输出内容不再打印到控制台,而是输出到指定的文件中

<strong><span style="font-size:24px;">package com.zken.testcase;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.PrintStream;public class TestSystemOut {public void redirectSystemOut() {try {System.setOut(new PrintStream(new FileOutputStream("systemOut.txt")));} catch (FileNotFoundException ex) {ex.printStackTrace();return;}System.out.println("Hello World!");}public static void main(String[] args) {new TestSystemOut().redirectSystemOut();}}</span></strong>


0 0
原创粉丝点击