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

mac2022-06-30  112

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

转载于:https://www.cnblogs.com/iamconan/p/7383594.html

最新回复(0)