Java中FileInputStream和BufferedInputStream的区别是什么?

Java中FileInputStream和BufferedInputStream的区别是什么?,java,Java,Java中FileInputStream和BufferedInputStream的区别是什么?主要区别: BufferedInputStream已被缓冲,但FileInputStream未被缓冲 BufferedInputStream从另一个InputStream读取,而FileInputStream从文件1读取 实际上,这意味着对FileInputStream.read()的每次调用都将执行一个系统调用(代价高昂)。。。而对BufferedInputStream.read()的大多数调用将

Java中FileInputStream和BufferedInputStream的区别是什么?

主要区别:

  • BufferedInputStream
    已被缓冲,但
    FileInputStream
    未被缓冲

  • BufferedInputStream
    从另一个
    InputStream
    读取,而
    FileInputStream
    从文件1读取

实际上,这意味着对
FileInputStream.read()
的每次调用都将执行一个系统调用(代价高昂)。。。而对
BufferedInputStream.read()
的大多数调用将从缓冲区返回数据。简而言之,如果您正在进行“小”读取,则将
BufferedInputStream
放入流堆栈将提高性能

  • 对于大多数目的/用例,这就是所有相关的内容

  • 还有一些其他的事情(如标记/重置/跳过),但这些都是相当专业的

  • 有关更多详细信息,请阅读。。。和源代码


1-或者更准确地说,来自某个对象,该对象1)在操作系统的“文件系统”命名空间中具有名称,2)操作系统允许您以字节序列的形式读取。这可能包括设备、命名管道和其他可能不被认为是“文件”的其他东西。还值得注意的是,有些东西是绝对不能用
文件输入流读取的

您必须这样做,或者阅读,

public class FileInputStream
extends InputStream
FileInputStream从文件系统中的文件获取输入字节。哪些文件可用取决于主机环境

FileInputStream用于读取原始字节流,如图像数据。要读取字符流,请考虑使用FieleRADER。

有关详细信息:

BufferedInputStream为另一个输入流添加了功能,即缓冲输入以及支持标记和重置方法的能力。创建BufferedInputStream时,将创建一个内部缓冲区数组。当读取或跳过流中的字节时,将根据需要从包含的输入流中重新填充内部缓冲区,每次填充多个字节。标记操作会记住输入流中的一个点,重置操作会导致在从包含的输入流中获取新字节之前,重新读取自最近的标记操作以来读取的所有字节

有关更多详细信息。

1,2c1,2
<公共类FileInputStream
<扩展输入流
---
>公共类BufferedInputStream
>扩展FilterInputStream
4,8c4,11
BufferedInputStream将功能添加到另一个输入流,即
>能够缓冲输入并支持标记和重置方法。当
>创建BufferedInputStream时,将创建一个内部缓冲区数组。作为字节
>从流中读取或跳过,内部缓冲区将重新填充为
>需要从包含的输入流中删除,一次需要多个字节。标记
>操作会记住输入流中的一个点,重置操作会导致
>自最近一次标记操作以来读取的所有字节将在新建之前重新读取
>字节取自包含的输入流。

合并和以获得答案。Erm可能重复-1表示演示,但+1表示厚颜无耻!
public class BufferedInputStream
extends FilterInputStream
1,2c1,2
< public class FileInputStream
< extends InputStream
---
> public class BufferedInputStream
> extends FilterInputStream
4,8c4,11
< A FileInputStream obtains input bytes from a file in a file system. What files
< are available depends on the host environment.
<
< FileInputStream is meant for reading streams of raw bytes such as image data.
< For reading streams of characters, consider using FileReader.
---
> A BufferedInputStream adds functionality to another input stream-namely, the
> ability to buffer the input and to support the mark and reset methods. When the
> BufferedInputStream is created, an internal buffer array is created. As bytes
> from the stream are read or skipped, the internal buffer is refilled as
> necessary from the contained input stream, many bytes at a time. The mark
> operation remembers a point in the input stream and the reset operation causes
> all the bytes read since the most recent mark operation to be reread before new
> bytes are taken from the contained input stream.