Search code examples
javaprintingconsolecommand-line-interfacesystem.out

Remove last character added to System.out


I'm writing a simple Client-Server protocol in java and am dealing with the server CLI right now. What I want to do is have a > at the beginning of the user input line. The function I have that prints the > character in console is consoleWriteln:

public static void consoleWriteln(String message) {
    System.out.println(message);
    System.out.print(">");
}

With this function, the server starts, but the character is displayed at the beginning of EVERY line.

Server running, listening on port 25565
>Tim has connected
>Boom
Server: Boom
>That's nice
Server: That's nice
>Tim: I agree!
>

The above function is called when anything comes in from a client, or something is broadcast from the server CLI.

How can I go about only having it on the user input line? IE:

Server running, listening on port 25565
Tim has connected
>Boom
Server: Boom
>That's nice
Server: That's nice
Tim: I agree!
>

Solution

  • you can catch the System.out, add a thread that listens to any output, process your task and then do setOut(PrintStream out)

    Then you make the server output directly to the PrintStream you used in setOut

    Here is some code in one of my projects: In the server you would do serverOut.println()...

        if(OUTPUT_IN_OVERLAY_CONSOLE || OUTPUT_IN_FILE || !OUTPUT_IN_CONSOLE) {
            try {
                JFrame console = new JFrame("Console");
                console.setSize(720,320);
                final JTextArea tarea = new JTextArea();
                JScrollPane scroll = new JScrollPane(tarea);
                console.add(scroll);                
    
                final OutputStream consoleOut = System.out;
                final OutputStream fileOut  = new FileOutputStream("output.txt");
                final OutputStream o = new OutputStream() {
                    StringBuilder sb = new StringBuilder();
                    @Override
                    public void write(int b) throws IOException {
                        sb.append((char)b);
                        tarea.setText(sb.toString());
                        tarea.setCaretPosition(sb.length());
                        //tarea.requestFocus();
                    }
                };
    
                OutputStream overrideOut = new OutputStream() {
    
                    @Override
                    public void write(int b) throws IOException {
                        if(OUTPUT_IN_CONSOLE) consoleOut.write(b);
                        if(OUTPUT_IN_OVERLAY_CONSOLE) o.write(b);
                        if(OUTPUT_IN_FILE)  fileOut.write(b);
                    }
                };
                System.setOut(new PrintStream(overrideOut));
    
                if(OUTPUT_IN_OVERLAY_CONSOLE) {
                    console.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                    console.setVisible(true);
                } 
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    

    Instead of doing System.setOut(new PrintStream(overrideOut));, create a class that extends PrintStream and override the println(String str) method to always add a ">" at the beginning