Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.io.IOException;
- /*
- * Clear console, i.e., push any existing content out of viewport.
- *
- * Command usage is static call:
- *
- * ClearScreen uses default value of lineFeeds, if default process
- * of clearing screen is required
- *
- * ClearScreen n overrides default value of lineFeeds to integer n to
- * be used in case that default process of clearing screen
- * is required
- *
- * @author: richarduie [at] yahoo [dot] com
- */
- public class ClearScreen {
- // system (OS) specific command to clear console - initialize to
- // default for Mac and Linux
- private static String clearCmd = "clear";
- // default number of line-feeds for brute-force screen-clearing
- // process of defaultClear() method
- private static int lineFeeds = 50;
- // args[0]...optional integer value to replace lineFeeds, in case
- // caller prefers some other number for default process
- public static void main(String[] args) {
- // reset from default, if argument given
- if (0 < args.length) setLineFeeds( args[ 0 ] );
- setClearCmd(); // set needed command by OS
- // if command could not be identified, use default process...
- if ( "default".equals( clearCmd ) ) defaultClear();
- // ...otherwise execute command
- else {
- try {
- // for read/write of stdout for command process
- String str;
- // execute command - will run in separate, external
- // process not directly impacting current console
- Process p = Runtime.getRuntime().exec( clearCmd );
- // read in process output associated with command
- BufferedReader in = new BufferedReader(
- new InputStreamReader( p.getInputStream( ) )
- );
- // write each line of external command process to
- // current console
- while ( null != (str = in.readLine( ))) {
- System.out.println( str );
- }
- }
- catch (IOException e) {
- defaultClear();
- }
- }
- }
- // for any console, assume entering lineFeed newlines pushes
- // existing content above top of viewport (brute-force approach)
- private static void defaultClear() {
- for (int i = 0; i < lineFeeds; i++) {
- System.out.print("\n");
- }
- }
- private static void setLineFeeds( String n ) {
- // attempt to reset lineFeeds from call argument
- try {
- lineFeeds = Integer.parseInt( n );
- }
- catch (NumberFormatException e) {
- // nothing to do - can't remedy caller's failure to provide
- // valid integer input; eat exception and leave default value
- // of lineFeeds unchanged as best guess
- ; // intentionally empty statement
- }
- }
- // by detected OS, determine needed command
- private static String setClearCmd() {
- String osName = System.getProperty("os.name").toLowerCase();
- boolean isWin = -1 != osName.indexOf( "win" );
- boolean isMac = -1 != osName.indexOf( "mac" );
- boolean isLin = -1 != osName.indexOf( "linux" );
- if (!( isWin || isMac || isLin) ) clearCmd = "default";
- else if (isWin) clearCmd = "cls";
- return clearCmd;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement