Java与Eclipse:能否同时输出内容到不同控制台?
Hey there! Since you're using Eclipse Oxygen on Windows for your first formal Java SE 8 course (with some hobby programming experience under your belt), let's get your output split into two distinct consoles—one for regular program outputs, another for behind-the-scenes info. Here's a straightforward, Eclipse-friendly way to do it:
Method 1: Leverage Eclipse's Built-in Console Filters (Simplest Approach)
This uses Eclipse's native console tools and avoids complex custom code, which is perfect for your current learning stage.
Step 1: Adjust Your Code to Use Separate Output Streams
Modify your code to send regular output to System.out and background info to System.err. This matches your pseudo-code structure:
public class DualOutputDemo { public static void main(String[] args) { // Regular output (matches your printToConsole1) System.out.println("Normal program outputs."); // Behind-the-scenes info (matches your printToConsole2) System.err.println("Behind the scenes information."); } }
Step 2: Create a Second Console View in Eclipse
- Run your program once—this will generate output in the default Console view.
- Click the Open Console button (the small
+icon) in the Console view's top-right corner. - Select New Console View from the dropdown. You’ll now have two separate Console panels in your Eclipse workspace.
Step 3: Filter Output for Each Console
For the first Console view (your regular output panel):
- Click the Display Selected Console dropdown (next to the
+icon) and select your running program (e.g.,DualOutputDemo [Java Application]). - Click the Filters button (funnel icon) and check only Standard Out, unchecking Standard Error. This console will now only show your regular program outputs.
- Click the Display Selected Console dropdown (next to the
For the second Console view (your background info panel):
- Repeat the first step to select your running program.
- Click the Filters button and check only Standard Error, unchecking Standard Out. This console will now exclusively display your behind-the-scenes messages.
Method 2: Custom Wrapper Methods (Optional, More Explicit)
If you want to use custom method names like your pseudo-code (printToConsole1/printToConsole2), you can wrap the stream calls for clearer, more intentional code:
import java.io.PrintStream; public class CustomConsoleWrapper { // Define your two output streams private static final PrintStream REGULAR_CONSOLE = System.out; private static final PrintStream BACKGROUND_CONSOLE = System.err; // Wrapper method for regular output public static void printToConsole1(String message) { REGULAR_CONSOLE.println(message); } // Wrapper method for background info public static void printToConsole2(String message) { BACKGROUND_CONSOLE.println(message); } public static void main(String[] args) { printToConsole1("Normal program outputs."); printToConsole2("Behind the scenes information."); } }
Then follow the same Console view setup steps from Method 1 to split the output.
This approach keeps your code clean and aligns with your original pseudo-code, while still using Eclipse's built-in tools to separate the consoles.
内容的提问来源于stack exchange,提问作者Ambrose2018

