Q
缓冲区和字节?
0
A
回答
2
缓冲区是在那里它被处理之前的数据临时存储在内存中的空间。请参阅Wiki article
下面介绍如何使用ByteBuffer类的简单Java example。
更新
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();
}
希望扫清事情了一点。
1
对于缓冲区,人们通常意味着要临时存储一些数据的一些内存块。缓冲区的一个主要用途是在I/O操作中。
像硬盘这样的设备擅长快速读取或写入磁盘上的连续位块。如果您告诉硬盘“读取这10,000个字节并将其放入内存中”,则可以非常快速地读取大量数据。如果你要编程一个循环并逐个获取字节,告诉硬盘每次获得一个字节,这将是非常低效和缓慢的。
因此,您创建一个10,000字节的缓冲区,告诉硬盘一次读取所有字节,然后从内存缓冲区中逐个处理这些10,000字节。
0
上的I/O的Sun Java教程节介绍了此话题:
http://java.sun.com/docs/books/tutorial/essential/io/index.html
相关问题
- 1. requirejs和字节缓冲区
- 2. 字节缓冲区,字符缓冲区,字符串和字符集
- 3. 解码字节缓冲区,
- 4. 32字节空缓冲区
- 5. Java NIO管道和字节缓冲区
- 6. Java字节缓冲区覆盖字节
- 7. 字节缓冲区开关字节序
- 8. 字节缓冲区,字符串
- 9. C++套接字256字节缓冲区
- 10. 字节缓冲区为字符串GWT
- 11. 使用编年史图和字节[]或字节缓冲区
- 12. 套接字和缓冲区
- 13. 将字节从一个字节缓冲区传输到另一个字节缓冲区
- 14. 字符和字节缓冲区编码和解码
- 15. Parquet Writer缓冲区或字节流
- 16. 将字节[]缓冲区重置为零?
- 17. 的Java字节缓冲区为String
- 18. 发送原始字节缓冲区后
- 19. Howto创建100M字节缓冲区
- 20. 字节缓冲区getInt()问题
- 21. 的NullPointerException在字节缓冲区
- 22. PHP中的字节缓冲区?
- 23. allocate_shared与附加的字节缓冲区
- 24. 字节缓冲区的等于
- 25. 字节缓冲区为String在Java中
- 26. Java中的字节缓冲区?
- 27. 获取的Java字节缓冲区内从字节低和高次半字节
- 28. 字节缓冲NSData
- 29. 字节/字符缓冲区长和/或双重
- 30. 的Java字节缓冲区“放”的方法 - 防止缓冲区溢出
我认为你需要更具体。到目前为止,这似乎并不特定于Java - 除非您指的是java.nio.ByteBuffer?你有什么特别的想法,你遇到了什么问题? – 2009-08-28 10:56:31