We know that the flow in Java is divided into character streams and byte streams, in which the character stream is mainly for the text processing, and the word throttling processing scope is more extensive, after all, pictures, movies, etc. as long as the file is in the form of binary, rather than characters.
Character streams:
FileReader
FileWriter.
BufferedReader
BufferedWriter
BYTE throttling:
FileInputStream
FileOutputStream
Bufferedinputstream
Bufferedoutputstream
Let's take a look at the J2SE documentation:
BufferedReader (Reader in): Creates a buffer character input stream that uses the default size input buffer.
You can see that BufferedReader is a reader of the subclass, the constructor is passed in the parent class reader class, in fact, is the decorator mode, the parent of the less efficient way to read the data to make a more effective method of buffer reading.
String ReadLine (): reads a line of text.
It has a method of ReadLine (), reading a row directly, which is more convenient than the read () method of the parent reader.
System.in: The corresponding standard input device: keyboard.
Static InputStream in: "Standard" input stream.
We can write a class to read data from the keyboard using the static method in of system:
<span style= "FONT-SIZE:18PX;" >import java.io.*;
Class readin{public
static void Main (string[] args) throws ioexception{inputstream inin
= system.in; Creates a new InputStream object, where the read position is keyboard
StringBuilder sb = new StringBuilder (); Used to store read characters, equivalent to building a buffer while
(true) {
int ch = inin.read (); Reads byte
if (ch== ' \ R ')
continue;
if (ch== ' \ n ') {
String s = sb.tostring ();//Read the carriage return to a String, see if it is "over", is the exit degree
if ("Over". Equals (s))
break;
else{
System.out.println (S.touppercase ());//not "over" words do not quit, a row of read finished, output to the screen
Sb.delete (0,sb.length ()); Empty cache StringBuilder sb
}
Else
sb.append ((char) ch); If you do not read the newline character, continue adding the read characters to the cache StringBuilder
}
}</span>
The type of the system in is InputStream, which belongs to the byte stream, and the bufferedreader belong to different flow classes. There is no full row read method such as ReadLine () in InputStream, only read ().
Then can not directly use the ReadLine () method to complete the keyboard input line of data reading.
That is, "can you flow bytes into a character stream, and then use the ReadLine method of the character stream buffer?"
OK.
There are such character streams in the character stream:
A bridge--inputstreamreader that converts byte flows to character streams;
InputStreamReader (InputStream in): Creates a inputstreamreader that uses the default character set. The object passed in is the InputStream type, and itself is a subclass of reader.
The--outputstreamwriter of a bridge that converts character flows to byte streams.
OutputStreamWriter (OutputStream out): Creates a outputstreamwriter that uses the default character encoding. The object passed in is the OutputStream type, and itself is a writer subclass.
As you can see from the name, they belong to the character stream reader and writer (the second half of the name), their function (see the first half of the name) and byte flow InputStream, OutputStream related.
<span style= "FONT-SIZE:18PX;"
>import java.io.*; Class transstreamdemo{public static void Main (string[] args) throws ioexception{inputstream in = system.in;
Gets the keyboard entry object. InputStreamReader ISR = new InputStreamReader (in); Converts a byte stream object into a character stream object, using a transform flow. InputStreamReader BufferedReader bufr = new BufferedReader (ISR); In order to improve efficiency, the string is efficiently manipulated by buffer technology. Use BufferedReader//BufferedReader BUFR = new BufferedReader (new InputStreamReader (system.in));
The most common way to write ...
OutputStream out = System.out;
OutputStreamWriter OSW = new OutputStreamWriter (out);
BufferedWriter BUFW = new BufferedWriter (OSW); BufferedWriter BUFW = new BufferedWriter (new OutputStreamWriter (System.out));
Ditto...
String line = null;
while (line = Bufr.readline ())!= null) {if (' over '. Equals (line)) break; Bufw.write (Line.touppercase ());
BUFW will OSW packaging, OSW will out of the packaging, in this case out is Syetem.out object implementation, so originally used System.out.println place directly can be wrapped in the BUFW output string. Bufw.newline (); Output line breaks: can be automatically exported according to different Linux and Windows \ n or \ r \ n BuFw.flush ();
Output stream has buffer, need flush to output to} bufr.close (); }}</span>
namely--
Read characters from a file, use FileReader, but in order to make it more efficient to read, use BufferedReader to wrap it;
Read the data from the keyboard (create a new InputStream object to get the keyboard input object,) convert the InputStream with the InputStreamReader conversion, and then use BufferedReader to wrap the inputstreamreader.
Of course InputStream can also read from the file, this time using is not inputstream in = system.in, but inputstream in = new FileInputStream (String fileName).
That
BufferedReader br = new BufferedReader (new InputStreamReader (system.in));
And
BufferedReader br = new BufferedReader (new InputStreamReader (New FileInputStream (String fileName));