如何将Java控制台输出读入字符串缓冲区

w1jd8yoj  于 2023-05-05  发布在  Java
关注(0)|答案(7)|浏览(110)

我有一个Java程序,它将一些文本输出到控制台。它使用printprintln和其他一些方法来实现这一点。
在程序结束时,我想读取控制台中的所有文本,并将其复制到String缓冲区中。在Java中如何做到这一点?我需要分别读取stdoutstderr

f45qwnt8

f45qwnt81#

这是一个有趣的问题。似乎不是一个优雅的方式来解决它的所有PrintStream方法一次。(可惜没有FilterPrintStream
不过,我确实写了一个丑陋的基于反射的解决方案(我想不会在生产代码中使用:)

class LoggedPrintStream extends PrintStream {

    final StringBuilder buf;
    final PrintStream underlying;

    LoggedPrintStream(StringBuilder sb, OutputStream os, PrintStream ul) {
        super(os);
        this.buf = sb;
        this.underlying = ul;
    }

    public static LoggedPrintStream create(PrintStream toLog) {
        try {
            final StringBuilder sb = new StringBuilder();
            Field f = FilterOutputStream.class.getDeclaredField("out");
            f.setAccessible(true);
            OutputStream psout = (OutputStream) f.get(toLog);
            return new LoggedPrintStream(sb, new FilterOutputStream(psout) {
                public void write(int b) throws IOException {
                    super.write(b);
                    sb.append((char) b);
                }
            }, toLog);
        } catch (NoSuchFieldException shouldNotHappen) {
        } catch (IllegalArgumentException shouldNotHappen) {
        } catch (IllegalAccessException shouldNotHappen) {
        }
        return null;
    }
}

。。。可以这样使用:

public class Test {
    public static void main(String[] args) {

        // Create logged PrintStreams
        LoggedPrintStream lpsOut = LoggedPrintStream.create(System.out);
        LoggedPrintStream lpsErr = LoggedPrintStream.create(System.err);

        // Set them to stdout / stderr
        System.setOut(lpsOut);
        System.setErr(lpsErr);

        // Print some stuff
        System.out.print("hello ");
        System.out.println(5);
        System.out.flush();

        System.err.println("Some error");
        System.err.flush();

        // Restore System.out / System.err
        System.setOut(lpsOut.underlying);
        System.setErr(lpsErr.underlying);

        // Print the logged output
        System.out.println("----- Log for System.out: -----\n" + lpsOut.buf);
        System.out.println("----- Log for System.err: -----\n" + lpsErr.buf);
    }
}

结果输出:

hello 5
Some error
----- Log for System.out: -----
hello 5

----- Log for System.err: -----
Some error

(Note但是,FilterOutputStream中的out字段是受保护和记录的,因此它是API的一部分:-)

7eumitmz

7eumitmz2#

一旦程序完成运行,您就无法执行此操作。您需要在程序开始写入输出之前执行此操作。
有关如何替换stdout和stderr的详细信息,请参阅this article(archive.org)。核心调用是System.setOut()System.setErr()

h4cxqtbf

h4cxqtbf3#

您可以使用PipedInputStream和PipedOutputStream。

//create pairs of Piped input and output streasm for std out and std err
final PipedInputStream outPipedInputStream = new PipedInputStream();
final PrintStream outPrintStream = new PrintStream(new PipedOutputStream(
    outPipedInputStream));
final BufferedReader outReader = new BufferedReader(
    new InputStreamReader(outPipedInputStream));
final PipedInputStream errPipedInputStream = new PipedInputStream();
final PrintStream errPrintStream = new PrintStream(new PipedOutputStream(
    errPipedInputStream));
final BufferedReader errReader = new BufferedReader(
    new InputStreamReader(errPipedInputStream));
final PrintStream originalOutStream = System.out;
final PrintStream originalErrStream = System.err;
final Thread writingThread = new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            System.setOut(outPrintStream);
            System.setErr(errPrintStream);
            // You could also set the System.in here using a
            // PipedInputStream
            DoSomething();
            // Even better would be to refactor DoSomething to accept
            // PrintStream objects as parameters to replace all uses of
            // System.out and System.err. DoSomething could also have 
            // an overload with DoSomething() calling: 
            DoSomething(outPrintStream, errPrintStream);
        } finally {
            // may also want to add a catch for exceptions but it is
            // essential to restore the original System output and error
            // streams since it can be very confusing to not be able to
            // find System.out output on your console
            System.setOut(originalOutStream);
            System.setErr(originalErrStream);
            //You must close the streams which will auto flush them
            outPrintStream.close();
            errPrintStream.close();
        }
    } // end run()
}); // end writing thread
//Start the code that will write into streams
writingThread.start();
String line;
final List<String> completeOutputStreamContent = new ArrayList<String>();
while ((line = outReader.readLine()) != null) {
    completeOutputStreamContent.add(line);
} // end reading output stream
final List<String> completeErrorStreamContent = new ArrayList<String>();
while ((line = errReader.readLine()) != null) {
    completeErrorStreamContent.add(line);
} // end reading output stream
ndasle7k

ndasle7k4#

下面是一个名为ConsoleOutputCapturer的实用程序类。它允许输出到现有的控制台,但在后台继续捕获输出文本。您可以使用start/stop方法控制要捕获的内容。换句话说,调用start开始捕获控制台输出,一旦捕获完成,就可以调用stop方法,该方法返回一个String值,其中包含start-stop调用之间的时间窗口的控制台输出。这个类不是线程安全的。

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.List;

public class ConsoleOutputCapturer {
    private ByteArrayOutputStream baos;
    private PrintStream previous;
    private boolean capturing;

    public void start() {
        if (capturing) {
            return;
        }

        capturing = true;
        previous = System.out;      
        baos = new ByteArrayOutputStream();

        OutputStream outputStreamCombiner = 
                new OutputStreamCombiner(Arrays.asList(previous, baos)); 
        PrintStream custom = new PrintStream(outputStreamCombiner);

        System.setOut(custom);
    }

    public String stop() {
        if (!capturing) {
            return "";
        }

        System.setOut(previous);

        String capturedValue = baos.toString();             

        baos = null;
        previous = null;
        capturing = false;

        return capturedValue;
    }

    private static class OutputStreamCombiner extends OutputStream {
        private List<OutputStream> outputStreams;

        public OutputStreamCombiner(List<OutputStream> outputStreams) {
            this.outputStreams = outputStreams;
        }

        public void write(int b) throws IOException {
            for (OutputStream os : outputStreams) {
                os.write(b);
            }
        }

        public void flush() throws IOException {
            for (OutputStream os : outputStreams) {
                os.flush();
            }
        }

        public void close() throws IOException {
            for (OutputStream os : outputStreams) {
                os.close();
            }
        }
    }
}
hts6caw3

hts6caw35#

之后不要这样做,在调用第一个System.out.print()之前创建两个StringBuilder对象,然后将您想要保存的每个字符串附加到相应的StringBuilder中。

hkmswyz6

hkmswyz66#

这两行代码将把你的输出放在一个文本文件中,或者你可以根据需要改变目标。
//创建一个文件:System.setOut(new PrintStream(new FileOutputStream(“D:/MyOutputFile.txt”)));//将输出重定向到文件:System.out.println(“Hello to custom output stream!“);
希望对你有所帮助。:)

csbfibhn

csbfibhn7#

通过实现模式装饰器并设置System.outSystem.err进行简单决策
执行

import java.io.PrintStream;

/** Simple implementation of pattern Decorator. */
public class SystemOutputProxy extends PrintStream {
    private final PrintStream printStream;
    private final StringBuilder sb;
    
    public SystemOutputProxy(PrintStream printStream, StringBuilder sb) {
        super(printStream);
        this.printStream = printStream;
        this.sb = sb;
    }
    
    @Override public void print(Object obj) {
        sb.append(obj);
        printStream.print(obj);
    }
    
    @Override public void print(String x) {
        sb.append(x);
        printStream.println(x);
    }
    
    
    /* override all others overloading of method: print(...) */
    
    
    @Override public void println(Object x) {
        sb.append(x).append(System.lineSeparator());
        printStream.println(x);
    }
    
    @Override public void println(String x) {
        sb.append(x).append(System.lineSeparator());
        printStream.println(x);
    }
    
    
    /* override all others overloading of method: println(...) */
    
    
    @Override public PrintStream printf(String format, Object... args) {
        sb.append(String.format(format, args));
        return printStream.printf(format, args);
    }
    
    
    /* override all others overloading of method: printf(...) */
    
    
    public PrintStream getPrintStream() {
        return printStream;
    }
     
}

使用showcase

public static void main(String[] args) {
        StringBuilder out = new StringBuilder();
        StringBuilder err = new StringBuilder();
        SystemOutputProxy proxyOut = new SystemOutputProxy(System.out, out);
        SystemOutputProxy proxyErr = new SystemOutputProxy(System.err, err);
        System.setOut(proxyOut);
        System.setErr(proxyErr);
        
        // do your code here...
        System.out.print("aaa");
        System.out.print("bbb");
        System.out.print("ccc");
        
        System.err.print("111");
        System.err.print("222");
        System.err.print("333");
        // finish your code...
        
        // set back original Output is not necessary
        System.setOut(proxyOut.getPrintStream());
        System.setErr(proxyErr.getPrintStream());
        
        boolean isOurContentEquals = out.toString().equals("aaabbbccc");
        boolean isErrContentEquals = err.toString().equals("111222333");
        
        System.out.println("isOurContentEquals = " + isOurContentEquals); // show true
        System.out.println("isErrContentEquals = " + isErrContentEquals); // show true
    }

控制台展示

aaa
bbb
ccc
isOurContentEquals = true
isErrContentEquals = true
111
222
333

相关问题