BufferedReader class in Java
Construction method:
1: public BufferedReader(Reader in,int sz )
1. Buffer character input stream with specified size input buffer;
2. in a Reader;
3. sz input buffer The size of the area.
2: public BufferedReader(Reader in)
1. Use the buffered character input stream of the default size input buffer.
Free video tutorial recommendation: java online tutorial
Commonly used methods:
public int read() throws IOException
Read a single character as an integer (range 0 to 65535 (0x00-0xffff)). If it reaches the end of the stream, -1 is returned.
public int read(char[] cbuf) throws IOException
Read one byte array cbuf at a time - the number of characters read from the target buffer, if it has been reached At the end of the stream, returns -1
public void close() throws IOException
Closes the stream and releases all resources associated with it.
Special usage:
public String readLine() throws IOException
1. Read a text line
2. One of the following characters can be considered to have terminated a line: line feed ('\n'), carriage return ('\r') or carriage return followed by a line feed.
3. If the end of the stream has been reached, return null
Sample code:
import java.io.BufferedReader; import java.io.FileReader; public class class1 { public static void main(String[] args) throws Exception { //创建字符缓冲输入流对象 BufferedReader br = new BufferedReader(new FileReader("D:/1.txt")); //读数据 //一次读取一个字符数组 char[] chs = new char[1024] ; int len = 0 ; while((len=br.read(chs))!=-1) { System.out.println(new String(chs,0,len)); } //释放资源 br.close(); } }
Recommended related articles and tutorials: java introductory tutorial
The above is the detailed content of Detailed explanation of the BufferedReader class in java. For more information, please follow other related articles on the PHP Chinese website!