1. java. the primary stream class in the io package-BufferedReader and BufferedWriter: BufferedReader: the cached input primary stream; BufferedWriter: the cached output primary stream; InputStreamReader: the input stream that converts bytes to characters; OutputStreamWriter: converts a character to a byte output stream. 2. instance-read characters:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BRRead { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub char c ; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); do{ c=(char)br.read(); System.out.println(c); }while(c!='q'); } }
By default, System. in is cached by row. That is to say, no input is passed to the application unless you press enter. The read () method is not ideal for interaction. 3. instance-read string:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BRReadLines { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String string; do{ string=br.readLine(); System.out.println(string); }while(!string.equals("stop")); } }
The program reads and displays text lines until the user enters the word "stop.