缓冲区和字节?
有人可以向我解释使用缓冲区的用途,也许还有一些使用缓冲区的简单(有记录的)示例。谢谢。
我对Java编程这方面的知识缺乏很多,所以如果我问错了问题,请原谅我。 :s
Could someone explain to me the uses of using buffers, and perhaps some simple (documented) examples of a buffer in use. Thanks.
I lack much knowledge in this area of Java programming, so forgive me if I asked the question wrong. :s
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
缓冲区是内存中的一个空间,数据在处理之前临时存储在其中。请参阅 Wiki 文章
这是一个简单的 如何使用 ByteBuffer 类的 Java 示例。
更新
希望这能让事情变得更清楚。
A buffer is a space in memory where data is stored temporarily before it is processed. See Wiki article
Heres a simple Java example of how to use the ByteBuffer class.
Update
Hope that clears things up a little.
对于缓冲区,人们通常指的是临时存储一些数据的某个内存块。缓冲区的一个主要用途是 I/O 操作。
像硬盘这样的设备擅长一次性快速读取或写入磁盘上的一组连续位。如果你告诉硬盘“读取这10,000字节并将它们放入内存中”,那么读取大量数据可以很快完成。如果你编写一个循环并逐个获取字节,告诉硬盘每次获取一个字节,这将是非常低效且缓慢的。
因此,您创建一个 10,000 字节的缓冲区,告诉硬盘一次性读取所有字节,然后从内存中的缓冲区中一一处理这 10,000 字节。
With a buffer, people usually mean some block of memory to temporarily store some data in. One primary use for buffers is in I/O operations.
A device like a harddisk is good at quickly reading or writing a block of consecutive bits on the disk in one go. Reading a large amount of data can be done very quickly if you tell the harddisk "read these 10,000 bytes and put them in memory here". If you would program a loop and get the bytes one by one, telling the harddisk to get one byte each time, it is going to be very inefficient and slow.
So you create a buffer of 10,000 bytes, tell the harddisk to read all the bytes in one go, and then you process those 10,000 bytes one by one from the buffer in memory.
有关 I/O 的 Sun Java 教程部分涵盖了此主题:
http ://java.sun.com/docs/books/tutorial/essential/io/index.html
The Sun Java tutorials section on I/O covers this topic:
http://java.sun.com/docs/books/tutorial/essential/io/index.html