1, use System.in.read read, use System.out.println output
1 PackageOrg.zln.io;2 3 Importjava.io.IOException;4 5 /**6 * Created by Coolkid on 2015/6/21 0021.7 */8 Public classtestsysteminreadstring {9 Public Static voidMain (string[] args)throwsIOException {Ten byte[] bytes =New byte[100]; One intLen = 0; ASystem.out.println ("Start reading"); - while(len = System.in.read (bytes)) >0){ -System.out.println ("Len:" +len); theSystem.out.write (Bytes, 0, Len); - if(len>=4&& "Exit". Equalsignorecase (NewString (bytes,0,4))){ - Break; - } + } -SYSTEM.OUT.PRINTLN ("End read"); + } A}E:\GitHub\tools\JavaEEDevelop\Lesson1_JavaSe_Demo1\src\org\zln\io\TestSystemInReadString.java
Analysis: The above code has a flaw, that is, each time (row) read data can not exceed 100 characters, more than the partial will be discarded.
2. Read with System.console.readLine
1 PackageOrg.zln.io;2 3 /**4 * Created by Coolkid on 2015/6/21 0021.5 */6 Public classTestConsole {7 Public Static voidMain (string[] args) {8 while(true){9System.out.print ("Start reading:");TenString s =System.Console (). ReadLine (); OneSystem.out.println ("read:" +s); A if(S.length () >3&& "Exit". Equalsignorecase (S.substring (0,4))){ - Break; - } the - } -SYSTEM.OUT.PRINTLN ("Read End"); - } +}E:\GitHub\tools\JavaEEDevelop\Lesson1_JavaSe_Demo1\src\org\zln\io\TestConsole.java
Analysis: Using the Console class, you must allow it in the console, not in the IDE. Because the virtual machine has a console that depends on the underlying platform and how the virtual machine is called
3, the password of the hidden input
1 Private Static voidConsolereadpassword () {2 while(true){3String s =NewString (System.Console (). Readpassword ("%s", "Please enter password:"));4System.out.println ("read:" +s);5 if(S.length () >3&& "Exit". Equalsignorecase (S.substring (0,4))){6 Break;7 }8 9 }TenSYSTEM.OUT.PRINTLN ("Read End"); One}E:\GitHub\tools\JavaEEDevelop\Lesson1_JavaSe_Demo1\src\org\zln\io\TestConsole.java
Analysis: The effect is the same as entering a password in the Linux console, that is, does not show how much password is entered, to some extent to prevent password disclosure
The system class for Java IO