Stream streams for Java

Source: Internet
Author: User

1. Read the console input

Java console input is completed by system.in. To get a character stream bound to the console, you can wrap the system.in in a BufferedReader object to create a character stream, and the syntax for creating BufferedReader is as follows:

BufferedReader br = new BufferedReader (new InputStreamReader (system.in));

After the BufferedReader object is created, a character can be read from the character stream through the Read () method, or a string is read from the character stream through the ReadLine () method.

 PackageCuiyuee;ImportJava.io.*; Public classBrread { Public Static voidMain (string[] args)throwsIOException {//TODO auto-generated Method Stub        CharC; BufferedReader BR=NewBufferedReader (NewInputStreamReader (system.in)); System.out.println ("Enter the character and press the Q key to exit. ");  Do{C= (Char) Br.read ();        System.out.println (c); } while(c! = ' Q '); }    }
 PackageCuiyuee;ImportJava.io.*; Public classBrreadline { Public Static voidMain (string[] args)throwsIOException {//TODO auto-generated Method StubBufferedReader br =NewBufferedReader (NewInputStreamReader (system.in));        String str; System.out.println ("Please enter a string and enter ' End ' to exit automatically:");  Do{str=Br.readline ();        System.out.println (str); } while(!str.equals ("End")); }}

2. Console output

The output of the console is done by print () and println (), which are defined by the class PrintStream, and System.out is a reference to the class object.

Printstrea inherits the OutputStream class, or writes to the console using the Write () method. But common print () and println ()

 Package Cuiyuee; import java.io.*;  Public class Writedemo {    publicstaticvoid  main (string[] args) {           TODO auto-generated method stub        int  b;         = ' A ';        System.out.write (b);        System.out.write (' \ n ');}    }

Stream streams for Java

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.