缓冲区和字节?

发布于 2024-08-03 09:28:12 字数 97 浏览 4 评论 0原文

有人可以向我解释使用缓冲区的用途,也许还有一些使用缓冲区的简单(有记录的)示例。谢谢。

我对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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

慕巷 2024-08-10 09:28:12

缓冲区是内存中的一个空间,数据在处理之前临时存储在其中。请参阅 Wiki 文章

这是一个简单的 如何使用 ByteBuffer 类的 Java 示例

更新

public static void main(String[] args) throws IOException 
{
    // reads in bytes from a file (args[0]) into input stream (inFile)
    FileInputStream inFile = new FileInputStream(args[0]);
    // creates an output stream (outFile) to write bytes to.
    FileOutputStream outFile = new FileOutputStream(args[1]);

    // get the unique channel object of the input file
    FileChannel inChannel = inFile.getChannel();
    // get the unique channel object of the output file.
    FileChannel outChannel = outFile.getChannel();    

    /* create a new byte buffer and pre-allocate 1MB of space in memory
       and continue to read 1mb of data from the file into the buffer until 
       the entire file has been read. */
    for (ByteBuffer buffer = ByteBuffer.allocate(1024*1024); inChannel.read(buffer) !=  1; buffer.clear()) 
    {
       // set the starting position of the buffer to be the current position (1Mb of data in from the last position)
       buffer.flip();
       // write the data from the buffer into the output stream
       while (buffer.hasRemaining()) outChannel.write(buffer);
    }

    // close the file streams.
    inChannel.close();
    outChannel.close();     
}

希望这能让事情变得更清楚。

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

public static void main(String[] args) throws IOException 
{
    // reads in bytes from a file (args[0]) into input stream (inFile)
    FileInputStream inFile = new FileInputStream(args[0]);
    // creates an output stream (outFile) to write bytes to.
    FileOutputStream outFile = new FileOutputStream(args[1]);

    // get the unique channel object of the input file
    FileChannel inChannel = inFile.getChannel();
    // get the unique channel object of the output file.
    FileChannel outChannel = outFile.getChannel();    

    /* create a new byte buffer and pre-allocate 1MB of space in memory
       and continue to read 1mb of data from the file into the buffer until 
       the entire file has been read. */
    for (ByteBuffer buffer = ByteBuffer.allocate(1024*1024); inChannel.read(buffer) !=  1; buffer.clear()) 
    {
       // set the starting position of the buffer to be the current position (1Mb of data in from the last position)
       buffer.flip();
       // write the data from the buffer into the output stream
       while (buffer.hasRemaining()) outChannel.write(buffer);
    }

    // close the file streams.
    inChannel.close();
    outChannel.close();     
}

Hope that clears things up a little.

云朵有点甜 2024-08-10 09:28:12

对于缓冲区,人们通常指的是临时存储一些数据的某个内存块。缓冲区的一个主要用途是 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.

乖乖哒 2024-08-10 09:28:12

有关 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文