- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我了解BufferedOutputStream
背后的理论。字节被写入缓冲区数组,直到它已满,然后写入(刷新)到底层流 - 这个想法比逐字节写入更快,因为操作系统调用更少。
但是,从 BufferedOutputStream
类和方法 (BufferedOutputStream.java) 的实现来看,似乎最终缓冲区中的字节只是逐字节写入。
我认为是这种情况,因为:
在 BufferedOutputStream.write(byte b[], int off, int len) 中有行 out.write(b, off, len)。由于 out 是 OutputStream 的一个实例,而不是 BufferedOutputStream,它正在调用 OutputStream.write(byte[], int, int)。这又使用for循环逐字节写入
请有人澄清实际发生了什么,以及它如何更快?
最佳答案
当数据被刷新时,它是一个 block 。
79 /** Flush the internal buffer */
80 private void flushBuffer() throws IOException {
81 if (count > 0) {
82 out.write(buf, 0, count);
83 count = 0;
84 }
85 }
FileOutputStream 和许多其他重写 OutputStream.write() 以有效地处理数据 block 。
http://www.docjar.com/html/api/java/io/FileOutputStream.java.html
284
285 /**
286 * Writes a sub array as a sequence of bytes.
287 * @param b the data to be written
288 * @param off the start offset in the data
289 * @param len the number of bytes that are written
290 * @param append {@code true} to first advance the position to the
291 * end of file
292 * @exception IOException If an I/O error has occurred.
293 */
294 private native void writeBytes(byte b[], int off, int len, boolean append)
295 throws IOException;
308 /**
309 * Writes <code>len</code> bytes from the specified byte array
310 * starting at offset <code>off</code> to this file output stream.
311 *
312 * @param b the data.
313 * @param off the start offset in the data.
314 * @param len the number of bytes to write.
315 * @exception IOException if an I/O error occurs.
316 */
317 public void write(byte b[], int off, int len) throws IOException {
318 writeBytes(b, off, len, append);
319 }
关于java - BufferedOutputStream 实际上是如何在底层工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9282909/
大家好,我遇到一些错误,所以我尝试使用 spring boot 和 React js 导入 excel 文件但在我的后端我收到错误 构造函数 BufferedOutputStream(FileOutp
我正在使用 BufferedOutputStream 写入文件。在调试时,我注意到即使操作系统显示文件已获得最终大小,刷新操作也会花费很长时间。 我的理论:BufferedOutputStream告诉
我正在尝试使用 TCP 开发一个简单的 Java 文件传输应用程序。我当前的服务器代码如下: package tcp.ftp; import java.io.BufferedInputStream;
我尝试创建一个 TFTP 服务器,但当它接收到一个文件时,似乎并非所有文件都保存到服务器上(缺少一些字节)。文件创建良好,大部分数据已写入,但由于文件不完整,因此被归类为损坏且无法打开。有谁知道如何解
我有几个问题- 1. 我有两台通过套接字连接的计算机。当程序执行时 outputStream.writeInt(value); outputStream.flush(); 究竟发生了什么?程序是否等到
我正在编写下载 servlet,它读取 html 文件并写入 servletOutputStream,传输的文件末尾的问题是添加一些垃圾数据,对此有何建议, 下面是我为此使用的代码 i
我制作的函数有一点问题。我希望每次我给这个函数一个字符串,它都会将我保存到同一文件中的新行,但实际上现在只保存我给出的最后一个字符串。这就像一次又一次的覆盖,需要一些帮助 public void Wr
我了解BufferedOutputStream 背后的理论。字节被写入缓冲区数组,直到它已满,然后写入(刷新)到底层流 - 这个想法比逐字节写入更快,因为操作系统调用更少。 但是,从 Buffered
我想知道 BufferedOutputStream 的用途,使用它时的性能提升? 最佳答案 这是来自 API of BufferedOutputStream 的行: The class impleme
我正在使用以下 Java 代码将一个字节写入文件 fileoutput.txt: FileOutputStream fos=new FileOutputStream("C:/tests/fileout
在Java中,我试图将服务器制作的每个BufferedOutputStream存储到线程之外的数组中,以便向所有连接的客户端广播一些数据。 // initialisation ArrayList
我有一个文件,其中包含一个写入文件开头的序列化 String 对象,后跟我尝试提取的文件的原始字节。 这是我的代码: FileInputStream fileInputStream = new Fil
这个问题已经有答案了: Java BufferedOutputStream vs OutputStream [duplicate] (1 个回答) 已关闭 4 年前。 在 java 8 中,以下之间有
这个问题已经有答案了: Sockets: BufferedOutputStream or just OutputStream? (1 个回答) 已关闭 8 年前。 BufferedOutputStre
我有多个实现 void convert(List list, OutputStream os) 方法。 给定的资源可以重用,因此方法实现不会释放它们。不过,在某些情况下,我想用 BufferedOu
我想使用 BufferedOutputStream 发送单独的消息。所以我做 bos.write(msg1); bos.flush(); bos.write(msg2); bos.flush()。另一
我试图在两端使用 BufferedOutputStream 和 BufferedInputStream 将文件从“服务器”发送到“客户端”。问题是,虽然我在每次写入(右侧)时刷新()服务器上的 Buf
我正在用 java 读取和写入图像文件。该文件可以是任何类型,所以我在写入图像时使用了缓冲读取器和写入器,但写入不正确。图像散落。 BufferedReader bufferedRea
我对 BufferedOutputStream 有疑问。我想通过套接字连接将 kml 文件从 Android 设备发送到 java 服务器。(连接正常,我已经可以在程序的其他部分与 PrintWrit
我正在尝试使用 Robert Sedgwick 在他的算法 4E 教科书中提供的 BinaryStdOut.java 类。该类的代码可以在他的网站上免费获得,但为了便于引用,我将在此处显示它的相关片段
我是一名优秀的程序员,十分优秀!