java - 针对 IO BufferInputStream 的 Nio 字节缓冲区(按 channel )

标签 java io nio

在哪种情况下,两者中哪一个是最佳选择? 显然,为非常小的文件使用文件 channel 是没有意义的。除此之外,两种输入/输出方式的优缺点是什么。 非常感谢。

最佳答案

FileChannel 有许多 java.io 中缺少的特性:它是可中断的,它可以在文件中移动位置,它可以锁定文件等。它可以比旧的 IO 更快,尤其是当它使用直接字节缓冲区时,这是 ByteBuffer API 的解释:

byte buffer is either direct or non-direct. Given a direct byte buffer, the Java virtual machine will make a best effort to perform native I/O operations directly upon it. That is, it will attempt to avoid copying the buffer's content to (or from) an intermediate buffer before (or after) each invocation of one of the underlying operating system's native I/O operations. 

如果您不需要上述任何功能与流一起使用,您将获得更短的代码。

关于java - 针对 IO BufferInputStream 的 Nio 字节缓冲区(按 channel ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16478076/

相关文章:

java - 在 (Drools) 中抛出自定义异常

java - 如何避免 Spring Roo GWT 支持?

C++从文本文件中读取,双行

java - 尝试让文件路径适用于 Java/Netbeans 中的所有版本

sockets - 触发按需 TCP 消息到可重新连接、不可共享的 Netty 管道的正确方法是什么?

Java - 无法创建具有 777 权限的目录(取而代之的是 775)

java - 如何使用WatchService观看多个目录?

java - 如何搜索 SQL 数据库并将其显示在 JTable 上

java - 在java中构建特定大小的信封对象?

io - 内存映射 IO 与 DMA?