Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.baeldung.clearconsolescreen;

import java.io.IOException;

public class ClearConsoleScreen {

public static void clearWithANSICodes() {
System.out.print("\033[H\033[2J");
System.out.flush();
}

public static void clearWithBlankLines() {
for (int i = 0; i < 50; i++) {
System.out.println();
}
}

public static void clearWithLinuxCommand() {
try {
new ProcessBuilder("clear")
.inheritIO()
.start()
.waitFor();
} catch (IOException | InterruptedException e) {
Thread.currentThread().interrupt();
}
}

public static void main(String[] args) {

System.out.println("This text appears first.");

clearWithANSICodes();
clearWithBlankLines();
clearWithLinuxCommand();

System.out.println("End of program output.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.baeldung.clearconsolescreen;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

public class ClearConsoleScreenUnitTest {

@Test
void givenAnsiClearMethod_whenInvoked_thenDoesNotThrowException() {
assertDoesNotThrow(ClearConsoleScreen::clearWithANSICodes);
}

@Test
void givenBlankLineClearMethod_whenInvoked_thenDoesNotThrowException() {
assertDoesNotThrow(ClearConsoleScreen::clearWithBlankLines);
}

@Test
void givenLinuxClearMethod_whenInvoked_thenDoesNotThrowException() {
assertDoesNotThrow(ClearConsoleScreen::clearWithLinuxCommand);
}
}