Java Stream and JavaStream
1. Read console input
The Java console input is completed by System. in. To obtain a consumer stream bound to the console, you can wrap System. in A BufferedReader object to create a consumer stream. The syntax for creating a BufferedReader is as follows:
BufferedReader br = new BufferedReader (new InputStreamReader (System. in ));
After a BufferedReader object is created, you can use the read () method to read a character from the primary stream, or read a string from the primary stream using the readLine () method.
Package cuiyuee; import java. io. *; public class BRRead {public static void main (String [] args) throws IOException {// TODO Auto-generated method stub char c; bufferedReader br = new BufferedReader (new InputStreamReader (System. in); System. out. println ("input character, press the q key to exit. "); Do {c = (char) br. read (); System. out. println (c);} while (c! = 'Q ');}}
Package cuiyuee; import java. io. *; public class BRReadLine {public static void main (String [] args) throws IOException {// TODO Auto-generated method stub BufferedReader br = new BufferedReader (new InputStreamReader (System. in); String str; System. out. println ("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 console outputs are completed by print () and println (). These methods are defined by the PrintStream class, and System. out is a reference of this class object.
PrintStrea inherits the OutputStream class and can use the write () method to write data to the console. But usually print () and println ()
package cuiyuee;import java.io.*;public class writeDemo { public static void main(String[] args) { // TODO Auto-generated method stub int b ; b = 'A'; System.out.write(b); System.out.write('\n'); }}