First, keyboard input
System.out: Standard output device, console
System.in: Standard input device, keyboard
Input Demo
Input string encountered carriage return, print, input over, input end
PS: Keyboard input Read Only one byte, the first byte to spell a string, so need a container, and then participate in the judgment of over
Import java.io.*;p Ublic class Main {public static void main (string[] args) throws IOException {InputStream in = system.in; StringBuilder sb = new StringBuilder (); int t = 0;while (true) {T = In.read (); Windows next car is ' \ r \ n ' if (t==) continue;//' \ r ' = 13if (t==10) {//' \ n ' = 10String str = sb.tostring (); if (Str.equals ("over")) Break System.out.println (str); Sb.delete (0, Sb.length ());//Note the situation, otherwise the string will grow longer}else {sb.append ((char) t);}} System.out.println (t);}}
Read (): is a blocking method, waiting without data
Note: The default input do not close, once closed, you can no longer create a stream object
Second, the conversion stream
Because there is a readline () method, it is not necessary to read a character one character, which involves converting a stream of characters to a character stream
There is a inputstreamreader () class in the reader class that is used to convert bytes and characters
API documentation explains: InputStreamReader is a bridge of byte flow to a character stream: It uses the specified charset
read byte and decodes it to a character.
And the origin of the character stream: Character stream + encoding table
Stream of byte-flow characters
Import java.io.*;p Ublic class Main {public static void main (string[] args) throws IOException {Indemo ();} public static void Indemo () throws Ioexception{inputstream in = system.in;//byte stream inputstreamreader ins = new Inputstrea Mreader (in);//convert character stream BufferedReader br = new BufferedReader (ins);//buffer, enhanced string str = Null;while ((str = Br.readline ())! = NULL) {if (Str.equals ("over")); System.out.println (str);}}}
byte stream of character flow
There are ouputstreamwriter classes under the writer class
API documentation explains: OutputStreamWriter is a bridge of character flow to a byte stream: it can be charset
encoded into bytes using the specified characters that will be written to the stream.
Import java.io.*;p Ublic class Main {private static String Line_separator = System.getproperty ("Line.separator");p ublic static void Main (string[] args) throws IOException {Indemo ();} public static void Indemo () throws Ioexception{/*inputstream in = system.in;//byte stream inputstreamreader ins = new Inputstr Eamreader (in);//convert character stream BufferedReader br = new BufferedReader (ins); */bufferedreader br = new BufferedReader (new InputStreamReader (system.in));/*outputstream out = system.out;//standard output stream OutputStreamWriter OSW = new OutputStreamWriter ( Out); BufferedWriter bw = new BufferedWriter (OSW); */bufferedwriter bw = new BufferedWriter (new OutputStreamWriter (System.out) ); String str = null;while (str = br.readline ())!=null) {if (Str.equals ("Over")) break;//writes the character data to the buffer with the buffer object, the final destination osw-> Out-> console Bw.write (str+line_separator); Bw.flush ();}}}
Iii. Conversion Flow Demo
Write data to a text file
Import java.io.*;p Ublic class Main {private static String Line_separator = System.getproperty ("Line.separator");p ublic static void Main (string[] args) throws IOException {Indemo ();} public static void Indemo () throws Ioexception{bufferedreader br = new BufferedReader (new InputStreamReader (system.in)); BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (New FileOutputStream ("Acm.txt")); String str = null;while ((str = br.readline ())!=null) {if (Str.equals ("Over")) Break;bw.write (Str+line_separator); Bw.flush ();}}}
Display a text file to the console
Import java.io.*;p Ublic class Main {private static String Line_separator = System.getproperty ("Line.separator");p ublic static void Main (string[] args) throws IOException {Indemo ();} public static void Indemo () throws Ioexception{bufferedreader br = new BufferedReader (New InputStreamReader Stream ("Acm.txt")); BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System.out)); String str = null;while ((str = br.readline ())!=null) {if (Str.equals ("Over")) Break;bw.write (Str+line_separator); Bw.flush ();}}}
Copy the contents of one file to another file
Import java.io.*;p Ublic class Main {private static String Line_separator = System.getproperty ("Line.separator");p ublic static void Main (string[] args) throws IOException {Indemo ();} public static void Indemo () throws Ioexception{bufferedreader br = new BufferedReader (New InputStreamReader Stream ("Acm.txt")); BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (New FileOutputStream ("A.txt")); String str = null;while ((str = br.readline ())!=null) {if (Str.equals ("Over")) Break;bw.write (Str+line_separator); Bw.flush ();}}}
The above three examples change only the source and destination of the stream
Java Learning Lesson 50th-io Flow (iv) conversion flow