📜  Java I/O-BufferedReader类

📅  最后修改于: 2020-09-27 04:51:22             🧑  作者: Mango

Java BufferedReader类

Java BufferedReader类用于从基于字符的输入流中读取文本。它可用于通过readLine()方法逐行读取数据。它使性能快速。它继承了Reader类。

Java BufferedReader类声明

让我们看一下Java.io.BufferedReader类的声明:

public class BufferedReader extends Reader

Java BufferedReader类构造函数

Constructor Description
BufferedReader(Reader rd) It is used to create a buffered character input stream that uses the default size for an input buffer.
BufferedReader(Reader rd, int size) It is used to create a buffered character input stream that uses the specified size for an input buffer.

Java BufferedReader类方法

Method Description
int read() It is used for reading a single character.
int read(char[] cbuf, int off, int len) It is used for reading characters into a portion of an array.
boolean markSupported() It is used to test the input stream support for the mark and reset method.
String readLine() It is used for reading a line of text.
boolean ready() It is used to test whether the input stream is ready to be read.
long skip(long n) It is used for skipping the characters.
void reset() It repositions the stream at a position the mark method was last called on this input stream.
void mark(int readAheadLimit) It is used for marking the present position in a stream.
void close() It closes the input stream and releases any of the system resources associated with the stream.

Java BufferedReader示例

在此示例中,我们使用Java BufferedReader类从文本文件testout.txt中读取数据。

package com.javatpoint;
import java.io.*;
public class BufferedReaderExample {
public static void main(String args[])throws Exception{  
  FileReader fr=new FileReader("D:\\testout.txt");  
  BufferedReader br=new BufferedReader(fr);  

  int i;  
  while((i=br.read())!=-1){
  System.out.print((char)i);
  }
  br.close();  
  fr.close();  
}  
}  

在这里,我们假设您在“ testout.txt”文件中包含以下数据:

Welcome to javaTpoint.

输出:

Welcome to javaTpoint.

通过InputStreamReader和BufferedReader从控制台读取数据

在此示例中,我们将BufferedReader流与InputStreamReader流连接,以从键盘读取一行一行的数据。

package com.javatpoint;
import java.io.*;
public class BufferedReaderExample{  
public static void main(String args[])throws Exception{    
InputStreamReader r=new InputStreamReader(System.in);  
BufferedReader br=new BufferedReader(r);    
System.out.println("Enter your name");  
String name=br.readLine();  
System.out.println("Welcome "+name);  
}  
}

输出:

Enter your name
Nakul Jain
Welcome Nakul Jain

从控制台读取数据直到用户写入停止的另一个示例

在此示例中,我们正在读取和打印数据,直到用户停止打印为止。

package com.javatpoint;
import java.io.*;
public class BufferedReaderExample{  
public static void main(String args[])throws Exception{    
 InputStreamReader r=new InputStreamReader(System.in);  
 BufferedReader br=new BufferedReader(r);    
 String name="";  
 while(!name.equals("stop")){  
  System.out.println("Enter data: ");  
  name=br.readLine();  
  System.out.println("data is: "+name);  
 }    
br.close();  
r.close();  
}  
}

输出:

Enter data: Nakul
data is: Nakul
Enter data: 12
data is: 12
Enter data: stop
data is: stop