📜  Java.io.BufferedReader Java中的类

📅  最后修改于: 2022-05-13 01:54:19.911000             🧑  作者: Mango

Java.io.BufferedReader Java中的类

从字符输入流中读取文本,缓冲字符,以便高效读取字符、数组和行。可以指定缓冲区大小,也可以使用默认大小。对于大多数用途,默认值足够大。通常,Reader 发出的每个读取请求都会导致对底层字符或字节流进行相应的读取请求。因此,建议将 BufferedReader 包装在 read() 操作可能成本高昂的任何 Reader 周围,例如 FileReaders 和 InputStreamReaders。使用 DataInputStreams 进行文本输入的程序可以通过将每个DataInputStream替换为适当的BufferedReader来本地化。

BufferedReader 类的构造函数

ConstructorAction Performed
BufferedReader(Reader in)Creates a buffering character-input stream that uses a default-sized input buffer
BufferedReader(Reader in, int sz)Creates a buffering character-input stream that uses an input buffer of the specified size.

BufferedReader 类的方法

Method NameAction 
close()Closes the stream and releases any system resources associated with it.Once the stream has been closed, further read(), ready(), mark(), reset(), or skip() invocations will throw an IOException. Closing a previously closed stream has no effect.
mark()Marks the present position in the stream. Subsequent calls to reset() will attempt to reposition the stream to this point.
markSupported()Tells whether this stream supports the mark() operation, which it does.
read()Reads a single character.
read(char[] cbuf, int off, int len)Reads characters into a portion of an array. This method implements the general contract of the corresponding read method of the Reader class. As an additional convenience, it attempts to read as many characters as possible by repeatedly invoking the read method of the underlying stream.
readLine()Reads a line of text. A line is considered to be terminated by any one of a line feed (‘\n’), a carriage return (‘\r’), or a carriage return followed immediately by a line feed.
ready()Tells whether this stream is ready to be read.
reset()Resets the stream to the most recent mark.
skip(long)Skips characters.

实现:文件里面的内容如下:

This is first line
this is second line

例子

Java
// Java Program to Illustrate BufferedReader Class
// Via Its Methods
  
// Importing required classes
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
  
// Class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
        throws IOException
    {
  
        // Creating object of FileReader and BufferedReader
        // class
        FileReader fr = new FileReader("file.txt");
        BufferedReader br = new BufferedReader(fr);
  
        char c[] = new char[20];
  
        // Illustrating markSupported() method
        if (br.markSupported()) {
  
            // Print statement
            System.out.println(
                "mark() method is supported");
  
            // Illustrating mark method
            br.mark(100);
        }
  
        // File Contents is as follows:
        // This is first line
        // this is second line
  
        // Skipping 8 characters
        br.skip(8);
  
        // Illustrating ready() method
        if (br.ready()) {
  
            // Illustrating readLine() method
            System.out.println(br.readLine());
  
            // Illustrating read(char c[],int off,int len)
            br.read(c);
  
            for (int i = 0; i < 20; i++) {
                System.out.print(c[i]);
            }
  
            System.out.println();
  
            // Illustrating reset() method
            br.reset();
            for (int i = 0; i < 8; i++) {
  
                // Illustrating read() method
                System.out.print((char)br.read());
            }
        }
    }
}


输出:

mark() method is supported
first line
this is second line
This is