1. BufferedReader class
. Class library:
java.lang.Object
java.io.Reader
java.io.BufferedReader
. Basic concepts:
public class BufferedReader extends Reader
Read text from the character input stream and buffer individual characters, thus enabling efficient reading of characters, arrays, and lines. You can specify the size of the buffer, or the default size can be used. In most cases, the default value is large enough.
Typically, each read request made by a Reader results in a corresponding read request for the underlying character or byte stream. Therefore, it is recommended to wrap all Readers whose read() operations can be expensive (such as FileReader and InputStreamReader) with BufferedReader.
The BufferedReader stream is able to read text lines and creates a BufferedReader object by passing a Reader object to the BufferedReader. This is because FileReader does not provide the function of reading text lines.
. Demo:
Capture the entered statement through the Bufferedreader:
import java.io.*;class BufferedReaderDemo{ public static void main(String[] args)throws IOException { BufferedReader bufferedReader =new BufferedReader( new InputStreamReader(System.in)); System.out.print("Please enter a series of text, which can include spaces: "); String text =bufferedReader.readLine(); System.out.println("Please enter text: "+text); } }annotation:
throws IOException throws exception
InputStreamReader is the bridge between byte flow and character flow
2. InputStreamReader class
InputStreamReader Converts a byte stream to a character stream. It is the bridge between byte flow and character flow. If character set encoding is not specified, the decoding process will use the platform's default character encoding, such as: GBK.
Construction method:
InputStreamReader isr = new InputStreamReader(InputStream in);//Construct an InputStreamReader class with the default encoding set
InputStreamReader isr = new InputStreamReader(InputStream in,String charsetName);//Construct a specified encoding set
InputStreamReader class.
The parameter in object is obtained by InputStream in = System.in;. //Read data on the keyboard.
Or InputStream in = new FileInputStream(String fileName);//Read the data in the file. It can be seen that FileInputStream is a subclass of InputStream.
Main method: int read();//Read a single character.
int read(char []cbuf);//Save the read characters into the array. Returns the number of characters read.
. Demo:
import java.io.*;class InputStreamReaderDemo { public static void transReadNoBuf() throws IOException { /** * There is no buffer, you can only use the read() method. */ //Read byte stream//InputStream in = System.in;//Read keyboard input. InputStream in = new FileInputStream("D://demo.txt");//Read the file data. //Conversion of flowing bytes to character stream. To enable effective conversion from bytes to characters, //You can read more bytes from the underlying stream in advance. InputStreamReader isr = new InputStreamReader(in);//Read//Comprehensive into one sentence. //InputStreamReader isr = new InputStreamReader( //new FileInputStream("D://demo.txt")); char []cha = new char[1024]; int len = isr.read(cha); System.out.println(new String(cha,0,len)); isr.close(); } public static void transReadByBuf() throws IOException { /** * Use buffers to use the read() and readLine() methods of the buffer object. */ //Read byte stream//InputStream in = System.in;//Read data on the keyboard InputStream in = new FileInputStream("D://demo.txt");//Read data on the file. //Conversion of flowing bytes to character stream. InputStreamReader isr = new InputStreamReader(in);//Read//Create character stream buffer BufferedReader bufr = new BufferedReader(isr);//Buffered//BufferedReader bufr = new BufferedReader( //new InputStreamReader(new FileInputStream("D://demo.txt"))); can be combined into one sentence. /*int ch = 0; ch = bufr.read(); System.out.println((char)ch); */ String line; while((line = bufr.readLine())!=null){ System.out.println(line); } isr.close(); }}3. Real cases of InputStreamReader and BufferedReader (non-coded set)
import java.io.*;class UtilResource { private void initializeResource() { try { //Read the file and write it out in utf-8 form BufferedReader bufread; String read; bufread = new BufferedReader(new InputStreamReader(ResourceHelper .getResourceInputStream("pinyin.txt"))); while ((read = bufread.readLine()) != null) { System.out.println(read); } bufread.close(); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } }}Note: pinyin.txt is placed in the root directory of the project
import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;class ResourceHelper { /** * @param resourceName * @return * @return */ static BufferedInputStream getResourceInputStream(String resourceName) { try { return new BufferedInputStream(new FileInputStream(resourceName)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }}Summarize:
InputStreamReader class
It is the bridge between byte flow and character flow, enclosing the InputStream inside. It reads characters one by one at a time in a higher level, inputs/outputs in text format, and can specify the encoding format;
BufferedReader class
BufferedReader is extended by the Reader class, providing a general buffered text reading, and provides a very practical readLine, which reads a text line, reads text from a character input stream, and buffers each character, thereby providing efficient reading of characters, arrays and lines.
ps: The relationship between InputStream, InputStreamReader and Reader
InputStream: The result is the byte input stream. After InputStream.read("filename"), the result is the byte stream.
Reader: Reading a character stream
InputStreamReader: A bridge from bytes to characters. InputStreamReader(InputStream.read("filename"));
reader.read(InputStreamReader(InputStream in)); can be changed from byte to character, and printed and displayed.
java.io.Reader and java.io.InputStream form the Java input class.
Reader is used to read 16-bit characters, that is, Unicode-encoded characters; while InputStream is used to read ASCII characters and binary data.
Reader supports 16-bit Unicode character output, and InputStream supports 8-bit character output.
Reader and InputStream are two sets of parallel and independent level mechanisms provided by the I/O library, respectively. 1byte = 8bits InputStream and OutputStream are used to process 8-bit streams, and Reader and Writer are used to process 16-bit streams.
In the JAVA language, the byte type is 8-bit and the char type is 16-bit, so you need to use Reader and Writer when processing Chinese.
It is worth noting that under these two levels of mechanisms, there is also a bridge InputStreamReader and OutputStreamWriter, which is responsible for the adaptation of InputStream to Reader and the adaptation of OutputStream to Writer.
In Java, there are different types of Reader input streams that correspond to different data sources:
FileReader is used for inputting from a file; CharArrayReader is used for inputting from a character array in the program; StringReader is used for inputting from a string in the program; PipedReader is used for reading data written to the pipeline from a PipedWriter in another thread.
There are also different types of InputStream input streams corresponding to different data sources: FileInputStream, ByteArrayInputStream, StringBufferInputStream, PipedInputStream.
In addition, there are two InputStream input streams that do not have a corresponding Reader type: Socket is used for sockets; URLConnection is used for URL connections. These two classes use getInputStream() to read the data.
Correspondingly, there are similar differences between java.io.Writer and java.io.OutputStream.
Regarding InputStream.read(byte[] b) and InputStream.read(byte[] b, int off, int len) both methods are used to read multiple bytes from a stream. Experienced programmers will find that these two methods often cannot read the number of bytes they want to read. For example, in the first method, programmers often hope that the program can read b.length bytes, but the actual situation is that the system often cannot read so many. After carefully reading the Java API instructions, you will find that this method does not guarantee that it can read so many bytes, it can only guarantee that it can read up to so many bytes (at least 1). Therefore, if you want the program to read count bytes, it is best to use the following code:
byte[] b = new byte[count]; int readCount = 0; // The number of bytes that have been successfully read while (readCount < count) { readCount += in.read(bytes, readCount, count - readCount); } This code can ensure that count bytes are read unless an IO exception is encountered in the middle or the end of the data stream (EOFException)