BufferedReader(Reader in, int size)
我已经编写了这个程序:
import java.io.*;
class bufferedReaderEx{
public static void main(String args[]){
InputStreamReader isr = null;
BufferedReader br = null;
try{
isr = new InputStreamReader(System.in);
// System.out.println("Write data: ");
// int i = isr.read();
// System.out.println("Data read is: " + i);
//Thus the InputStreamReader is useful for reading the character from the stream
System.out.println("Enter the data to be read by the bufferedReader: ");
//here isr is containing the lnefeed already so this is needed to be flushed.
br = new BufferedReader(isr, 2);
String str = br.readLine();
System.out.println("The data is : :" + str);
}catch(IOException e){
System.out.println("Can't read: " + e.getMessage());
}
}
}
输出:
Enter the data to be read by the bufferedReader: Hello world and hello world again
The data is: Hello world and hello world again
那么缓冲大小是什么意思呢?我本来想让它只阅读两个字符,但实际上并不是这样的。
byte[] buffer = new byte[inputstream.available()]
,那么这将是最快的,对吗?但是,我们经常看到的是使用默认大小、2048或1024的缓冲区,而不是找到实现这种方式的人。 - WesternGun