Monday, February 5, 2024

Clear Screen

Sometimes when you're writing a program in Java you want to clear the screen. That is to remove all text from the screen.

Here's how we would do this in Java:

private void clearScreen() {
        try {
            if (System.getProperty("os.name").contains("Windows")) {
                new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
            } else {
                new ProcessBuilder("clear").inheritIO().start().waitFor();
            }
        } catch (IOException | InterruptedException ex) {
            ex.printStackTrace();
        }
    }

This takes into account both windows and non windows. cls for Windows, clear for linux etc.

No comments:

Post a Comment

We Live In A Simulation

 You read that title right. We live in a simulation. Nothing is real. It's as real as you want it to be. But that doesn't mean it...