InputStreamReader和BufferedReader的区别?

lpj003 2005-11-14 11:27:38
char ch;
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);

ch = (char)in.read();和ch = (char)br.read();这两句有什么区别?

即InputStreamReader.read()和BufferedReader.read()方法的区别是什么?
...全文
797 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
believefym 2005-11-14
  • 打赏
  • 举报
回复
BufferedReader:
Read text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.

------------------------------
InputStreamReader:
An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset.
lpj003 2005-11-14
  • 打赏
  • 举报
回复
哪个读字节?哪个读字符?
chw8219 2005-11-14
  • 打赏
  • 举报
回复
同意楼上的.
不过上面给那么多英文说明,看得累啊!
我们汉字是2个字节的吗,如果读字节,万一不当会出错啦,而读字符就不会啦.
schee 2005-11-14
  • 打赏
  • 举报
回复
一个是读字节,一个是字符.
believefym 2005-11-14
  • 打赏
  • 举报
回复
public class BufferedReaderextends ReaderRead text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.

The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.

In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. For example,

BufferedReader in
= new BufferedReader(new FileReader("foo.in"));
will buffer the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient.
Programs that use DataInputStreams for textual input can be localized by replacing each DataInputStream with an appropriate BufferedReader.


-------------------------

public class InputStreamReaderextends ReaderAn InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.

Each invocation of one of an InputStreamReader's read() methods may cause one or more bytes to be read from the underlying byte-input stream. To enable the efficient conversion of bytes to characters, more bytes may be read ahead from the underlying stream than are necessary to satisfy the current read operation.

For top efficiency, consider wrapping an InputStreamReader within a BufferedReader. For example:

BufferedReader in
= new BufferedReader(new InputStreamReader(System.in));
/** * 一、BufferedReader类 public class BufferedReader extends Reader * 从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。 可以指定缓冲区的大小,或者可使用默认的大小。大多数情况下,默认值足够大。 * 通常,Reader 所作的每个读取请求都会导致对底层字符或字节流进行相应的读取请求。因此,建议用 BufferedReader包装所有其 read() * 操作可能开销很高的 Reader(如 FileReader和 InputStreamReader)。 * BufferedReader流能够读取文本行,通过向BufferedReader传递一个Reader对象 * ,来创建一个BufferedReader对象,之所以这样做是因为FileReader没有提供读取文本行的功能. * * 二、InputStreamReader类 * * InputStreamReader 将字节流转换为字符流。是字节流通向字符流的桥梁。如果不指定字符集编码,该解码过程将使用平台默认的字符编码,如:GBK。 * * 构造方法: * * InputStreamReader isr = new InputStreamReader(InputStream * in);//构造一个默认编码集的InputStreamReader类 * * InputStreamReader isr = new InputStreamReader(InputStream in,String * charsetName);//构造一个指定编码集的InputStreamReader类。 * * 参数 in对象通过 InputStream in = System.in;获得。//读取键盘上的数据。 * * 或者 InputStream in = new FileInputStream(String fileName);//读取文件中的数据。可以看出 * FileInputStream 为InputStream的子类。 * * 主要方法:int read();//读取单个字符。 int read(char []cbuf);//将读取到的字符存到数组中。返回读取的字符数。 * * 三、FileWriter(少量文字) 和 BufferedWriter(大量文字)实现简单文件写操作 * @author hulk */

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧