java控制台怎么打印,Java:打印到控制台和文件

这篇博客讨论了如何在Java程序中同时将输出既显示在控制台又写入文件。通过创建自定义的PrintStream或者使用现有的logging库,可以实现这种双重输出。示例代码展示了创建一个类来扩展PrintStream,以便在控制台和文件中同时打印信息。作者建议使用操作系统提供的重定向功能或采用成熟的日志解决方案,如Log4J,而不是自己实现。
摘要由CSDN通过智能技术生成

I have a bit of a problem

I am running a program with many classes, with one driver to run all these classes. When I run the driver, the output prints on the console just fine, but I also need the exact output from the console to be printed to a file.

However, I can't seem to get the console output to print to a file. I have tried the suggestion here

By doing this

import java.io.FileOutputStream;

import java.io.PrintStream;

public class Compiler {

public static void main(String args[]){

try {

Parse parser = new Parse();

SemanticAnalyzer sAnalyzer = new SemanticAnalyzer();

String parserfile= parser.parsefile(args[0]);

sAnalyzer.semanticAnalysis(parserfile);

PrintStream out = new PrintStream(new FileOutputStream("output.txt"));

Syste

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值