Advertisement
richarduie

ClearScreen.java

Jun 2nd, 2013
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.04 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.io.IOException;
  4.  
  5. /*
  6.  * Clear console, i.e., push any existing content out of viewport.
  7.  *
  8.  * Command usage is static call:
  9.  *
  10.  * ClearScreen      uses default value of lineFeeds, if default process
  11.  *                  of clearing screen is required
  12.  *
  13.  * ClearScreen n    overrides default value of lineFeeds to integer n to
  14.  *                  be used in case that default process of clearing screen
  15.  *                  is required
  16.  *
  17.  * @author: richarduie [at] yahoo [dot] com
  18.  */
  19.  
  20. public class ClearScreen {
  21.     // system (OS) specific command to clear console - initialize to
  22.     // default for Mac and Linux
  23.     private static String clearCmd = "clear";
  24.  
  25.     // default number of line-feeds for brute-force screen-clearing
  26.     // process of defaultClear() method
  27.     private static int lineFeeds = 50;
  28.  
  29.     // args[0]...optional integer value to replace lineFeeds, in case
  30.     //           caller prefers some other number for default process
  31.     public static void main(String[] args) {
  32.         // reset from default, if argument given
  33.         if (0 < args.length) setLineFeeds( args[ 0 ] );
  34.        
  35.         setClearCmd();  // set needed command by OS
  36.  
  37.         // if command could not be identified, use default process...
  38.         if ( "default".equals( clearCmd ) ) defaultClear();
  39.         // ...otherwise execute command
  40.         else {
  41.             try {
  42.                 // for read/write of stdout for command process
  43.                 String str;
  44.                 // execute command - will run in separate, external
  45.                 // process not directly impacting current console
  46.                 Process p = Runtime.getRuntime().exec( clearCmd );
  47.                 // read in process output associated with command
  48.                 BufferedReader in = new BufferedReader(
  49.                     new InputStreamReader( p.getInputStream( ) )
  50.                 );
  51.                 // write each line of external command process to
  52.                 // current console
  53.                 while ( null != (str = in.readLine( ))) {
  54.                     System.out.println( str );
  55.                 }
  56.             }
  57.             catch (IOException e) {
  58.                 defaultClear();
  59.             }
  60.         }
  61.     }
  62.  
  63.     // for any console, assume entering lineFeed newlines pushes
  64.     // existing content above top of viewport (brute-force approach)
  65.     private static void defaultClear() {
  66.         for (int i = 0; i < lineFeeds; i++) {
  67.             System.out.print("\n");
  68.         }
  69.     }
  70.     private static void setLineFeeds( String n ) {
  71.         // attempt to reset lineFeeds from call argument
  72.         try {
  73.             lineFeeds = Integer.parseInt( n );
  74.         }
  75.         catch (NumberFormatException e) {
  76.             // nothing to do - can't remedy caller's failure to provide
  77.             // valid integer input; eat exception and leave default value
  78.             // of lineFeeds unchanged as best guess
  79.             ;   // intentionally empty statement
  80.         }
  81.     }
  82.     // by detected OS, determine needed command
  83.     private static String setClearCmd() {
  84.         String osName = System.getProperty("os.name").toLowerCase();
  85.         boolean isWin = -1 != osName.indexOf( "win" );
  86.         boolean isMac = -1 != osName.indexOf( "mac" );
  87.         boolean isLin = -1 != osName.indexOf( "linux" );
  88.         if (!( isWin || isMac || isLin) ) clearCmd = "default";
  89.         else if (isWin) clearCmd = "cls";
  90.         return clearCmd;
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement