package cn.fateverse.query;

import java.io.*;

   public class ConsoleCapture {
       public static String captureOutput(Runnable codeToRun) throws IOException {
           PrintStream originalOut = System.out;
           ByteArrayOutputStream baos = new ByteArrayOutputStream();
           PrintStream capturingOut = new PrintStream(baos);

           try {
               System.setOut(capturingOut);
               codeToRun.run();
           } finally {
               System.setOut(originalOut);
           }

           return baos.toString();
       }

       // 使用示例
       public static void main(String[] args) throws IOException {
           String capturedText = captureOutput(() -> {
               System.out.println("Hello, Console!");
               System.out.println("This is being captured.");
           });

           System.out.println("Captured output:");
           System.out.println(capturedText);
       }
   }
   