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

该代码可以实现让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>


posted @ 2015-06-25 17:42  crazy_itman  阅读(849)  评论(0编辑  收藏  举报