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