Packagecom.test.Log4j; ImportJava.io.BufferedReader; ImportJava.io.Console; Importjava.io.IOException; ImportJava.io.InputStreamReader; ImportJava.util.Scanner; ImportJavax.swing.JOptionPane; Importjunit.framework.TestCase; Public classInputtestextendsTestCase {/*** Using byte input classes*/ Public voidTestbytereader () {String s= ""; System.out.println ("Bytereader Mode Input"); byte[] Readin =New byte[50]; intCount = 0; Try{System.out.println ("You input:"); Count=System.in.read (Readin); }Catch(Exception e) {e.printstacktrace (); } System.out.println (NewString (Readin, 0, Count)); } /*** using character input stream*/ Public voidTestbufferedreader () {String s= ""; BufferedReader BR=NewBufferedReader (NewInputStreamReader (system.in)); System.out.println ("Bufferreader Mode Input"); Try{s=Br.readline (); } Catch(IOException e) {e.printstacktrace (); } System.out.println (s); } /*** Scanner Class (Scanner) Read string from console*/ Public voidTestscanner () {String s= ""; Scanner SC=NewScanner (system.in); System.out.println ("Scanner Mode input"); S=Sc.next (); System.out.println (s); } /*** dialog box input data*/ Public voidTestjoptionpane () {System.out.println ("Joptionpane Mode Input"); String name= Joptionpane.showinputdialog ("What is your name?")); SYSTEM.OUT.PRINTLN (name); } Public voidTestConsole () {/**jdk1.6 New Features * Java to interact with the console, it is not always possible to get the Java * Console class available. Whether a JVM has an available console depends on how the underlying platform and JVM are called *. If the JVM was started in an interactive command line (such as Windows CMD *) and the input output is not redirected to another location, then we can get an available console instance. * When you run the above code in eclipse or Netbean, the following text output will be available in the console: console is unavailable. * * indicates that the Java program could not get the console instance because the JVM was not invoked on the command line, or the input output was redirected. */Console Console=System.Console (); if(Console = =NULL) { Throw NewIllegalStateException ("Cannot use console"); } String S= Console.ReadLine ("Enter your parameters"); System.out.println (s); } Public Static voidMain (string[] args) {/*** Main function parameter input*/System.out.println (args[0]); } }
Five ways to type in Java----