Java BufferedReader 可以读取字节吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22216398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can BufferedReader read bytes?
提问by TruePS
Sorry if this question is a dulplicate but I didn't get an answer I was looking for.
对不起,如果这个问题是重复的,但我没有得到我正在寻找的答案。
Java docs says this
Java 文档这样说
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,
通常,Reader 发出的每个读取请求都会导致对底层字符或字节流发出相应的读取请求。因此,建议将 BufferedReader 包装在任何 read() 操作可能代价高昂的 Reader 周围,例如 FileReaders > 和 InputStreamReaders。例如,
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.
将缓冲来自指定文件的输入。如果没有缓冲,每次调用 read() 或 readLine() 都可能导致从文件中读取字节,转换为字符,然后返回,这可能非常低效。
My first question isIf bufferedReader can read bytes then why can't we work on images which are in bytes using bufferedreader.
My second question isDoes Bufferedreader store characters in BUFFER and what is the meaning of this line
我的第一个问题是,如果 bufferedReader 可以读取字节,那么为什么我们不能使用 bufferedreader 处理以字节为单位的图像。
我的第二个问题是Bufferedreader 是否将字符存储在 BUFFER 中,这一行的含义是什么
will buffer the input from the specified file.
将缓冲来自指定文件的输入。
- My third question iswhat is the meaning of this line
- 我的第三个问题是这行是什么意思
In general, each read request made of a Reader causes a corresponding read request to be >made of the underlying character or byte stream.
通常,由 Reader 发出的每个读取请求都会导致由底层字符或字节流构成的相应读取请求。
采纳答案by UVM
Default behaviour is it will convert to character, but when you have an image you cannot have a character data, instead you need pixel of bytes data. So you cannot use it.
It is buffereing, means , it is reading a certain chunk of data in an char array. You can see this behaviour in the code:
默认行为是它将转换为字符,但是当您拥有图像时,您不能拥有字符数据,而是需要像素的字节数据。所以你不能使用它。
它正在缓冲,意味着它正在读取字符数组中的某个数据块。您可以在代码中看到这种行为:
public BufferedReader(Reader in) { this(in, defaultCharBufferSize); }
public BufferedReader(Reader in) { this(in, defaultCharBufferSize); }
and the defaultCharBufferSize is as mentioned below:
和 defaultCharBufferSize 如下所述:
private static int defaultCharBufferSize = 8192;
私有静态 int defaultCharBufferSize = 8192;
3 Every time you do read operation, it will be reading only one character.
So in a nutshell, buffred means, it will read few chunk of character data first that will keep in a char array and that will be processed and again it will read same chunk of data until it reaches end of stream
因此,简而言之,缓冲意味着,它将首先读取少量字符数据块,这些数据块将保存在一个字符数组中并进行处理,然后再次读取相同的数据块,直到到达流的末尾
You can refer the following to get to know more
您可以参考以下了解更多
回答by radai
Readers (and Writers) in java are specialized classes for dealing with text (character) streams - the concept of a line is meaningless in any other type of stream.
Java 中的读取器(和写入器)是用于处理文本(字符)流的专门类 - 行的概念在任何其他类型的流中都没有意义。
for the general IO equivalent, have a look at BufferedInputStream
对于一般的 IO 等效项,请查看BufferedInputStream
so, to answer your questions:
所以,回答你的问题:
- while the reader does eventually read bytes, it converts them to characters. it is not intended to read anything else (like images) - use the InputStream family of classes for that
- a buffered reader will read large blocks of data from the underlying stream (which may be a file, socket, or anything else) into a buffer in memory and will then serve read requests from this buffer until the buffer is emptied. this behaviour of reading large chunks instead of smaller chucks every time improves performance.
- it means that if you dontwrap a reader in a buffered reader then every time you want to read a single character, it will access the disk.network to get just the single character you want. doing I/O in such small chunks is usually terrible for performance.
- 虽然读取器最终会读取字节,但它会将它们转换为字符。它不打算读取任何其他内容(如图像) - 为此使用 InputStream 系列类
- 缓冲读取器将从底层流(可能是文件、套接字或其他任何东西)中读取大块数据到内存中的缓冲区中,然后将处理来自该缓冲区的读取请求,直到该缓冲区被清空。这种每次读取大块而不是小卡盘的行为提高了性能。
- 这意味着如果您不将读取器包装在缓冲读取器中,那么每次您想要读取单个字符时,它都会访问 disk.network 以获取您想要的单个字符。在如此小的块中进行 I/O 通常对性能来说很糟糕。
回答by slim
There are two questions here.
这里有两个问题。
1. Buffering
1. 缓冲
Imagine you lived a mile from your nearest water source, and you drink a cup of water every hour. Well, you wouldn't walk all the way to the water for every cup. Go once a day, and come home with a bucket full of enough water to fill the cup 24 times.
想象一下,你住在离最近的水源一英里的地方,你每小时喝一杯水。好吧,你不会每杯都走到水边。每天去一次,然后带着一桶足够装满杯子 24 次的水回家。
The bucket is a buffer.
桶是一个缓冲区。
Imagine your village is supplied water by a river. But sometimes the river runs dry for a month; other times the river brings so much water that the village floods. So you build a dam, and behind the dam there is a reservtheitroad. The reservtheitroad fills up in the rainy season and gradually empties in the dry season. The village gets a steady flow of water all year round.
想象一下,您的村庄由一条河流供水。但有时河流干涸一个月;有时,这条河带来的水太多,以至于村庄被洪水淹没。所以你建一个大坝,在大坝后面有一个水库。雨季蓄水,旱季逐渐排空。村里一年四季都有稳定的水流。
The reservtheitroad is a buffer.
水库是一个缓冲区。
Data streams in computing are similar to both those scenarios. For example, you can get several kilobytes of data from a filesystem in a single OS system call, but if you want to process one character at a time, you need something similar to a reservtheitroad.
计算中的数据流类似于这两种情况。例如,您可以在单个 OS 系统调用中从文件系统中获取几千字节的数据,但是如果您想一次处理一个字符,则需要类似于存储库的东西。
A BufferedReader contains within it another Reader (for example a FileReader), which is the river -- and an array of bytes, which is the reservtheitroad. Every time you read from it, it does something like:
BufferedReader 中包含另一个 Reader(例如 FileReader),它是河流——以及一个字节数组,它是水库。每次您阅读它时,它都会执行以下操作:
if there are not enough bytes in the "reservtheitroad" to fulfil this request
top up the "reservtheitroad" by reading from the underlying Reader
endif
return some bytes from the "reservtheitroad".
Howeverwhen you usea BufferedReader, you don't need to know howit works, only that it works.
然而,当你使用BufferedReader 时,你不需要知道它是如何工作的,只需要知道它是如何工作的。
2. Suitability for images
2. 图像适用性
It's important to understand that BufferedReader and FileReader are examples of Readers. You might not have covered polymorphism in your programming education yet, so when you do, remember this. It means that if you have code which uses FileReader-- but only the aspects of it that conform to Reader-- then you can substitute a BufferedReaderand it will work just the same.
理解 BufferedReader 和 FileReader 是Readers 的例子很重要。您可能还没有在编程教育中涉及多态性,所以当您这样做时,请记住这一点。这意味着,如果您有使用的代码FileReader——但只有符合的方面Reader——那么您可以替换 aBufferedReader并且它的工作方式相同。
It's a good habit to declare variables as the most general class that works:
将变量声明为最通用的类是一个好习惯:
Reader reader = new FileReader(file);
... because then this would be the only change you need to add buffering:
...因为这将是您需要添加缓冲的唯一更改:
Reader reader = new BufferedReader(new FileReader(file));
I took that detour because it's all Readers that are less suitable for images.
我走了那条弯路,因为这Reader都是不太适合图像的地方。
Readerhas two readmethods:
Reader有两种read方法:
int read(); // returns one character, cast to an int
int read(char[] block); // reads into block, returns how many chars it read
The second form is unsuitable for images because it definitely reads chars, not ints.
第二种形式不适合图像,因为它肯定读取字符,而不是整数。
The first form looks as if it might be OK -- after all, it reads ints. And indeed, if you just use a FileReader, it might well work.
第一种形式看起来好像没问题——毕竟,它读取的是整数。事实上,如果你只使用 FileReader,它可能会很好地工作。
However, think about how a BufferedReader wrapped around a FileReader will work. The first time you call BufferedReader.read(), it will call FileReader.read(buffer) to fill its buffer. Then it will cast the first charof the buffer back to an int, and return that.
但是,考虑一下环绕 FileReader 的 BufferedReader 将如何工作。第一次调用 BufferedReader.read() 时,它将调用 FileReader.read(buffer) 来填充其缓冲区。然后它将char缓冲区的第一个转换回一个 int,并返回它。
Especially when you bring multi-byte charsets into the picture, that can cause problems.
特别是当您将多字节字符集引入图片时,这可能会导致问题。
So if you want to read integers, use InputStreamnot Reader. InputStreamhas int read(byte[] buf, int offset, int length)-- bytes are much more reliably cast back and forth from int than chars.
因此,如果您想读取整数,请使用InputStreamnot Reader。InputStreamhas int read(byte[] buf, int offset, int length)-- 字节比字符更可靠地从 int 来回转换。

