The system class for Java IO

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.