Java 中的 FileInputStream 和 BufferedInputStream 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21641551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between FileInputStream and BufferedInputStream in Java?
提问by Rohit
What is the difference between FileInputStream and BufferedInputStream in Java?
Java 中的 FileInputStream 和 BufferedInputStream 有什么区别?
回答by Stephen C
Key differences:
主要区别:
BufferedInputStream
is buffered, butFileInputStream
is not.A
BufferedInputStream
reads from anotherInputStream
, but aFileInputStream
reads from a file1.
BufferedInputStream
被缓冲,但FileInputStream
不是。A
BufferedInputStream
从另一个读取InputStream
,但 aFileInputStream
从文件1读取。
In practice, this means that every call to FileInputStream.read()
will perform a syscall (expensive) ... whereas most calls to BufferedInputStream.read()
will return data from the buffer. In short, if you are doing "small" reads, putting a BufferedInputStream
into your stream stack will improve performance.
实际上,这意味着每次调用FileInputStream.read()
都会执行系统调用(昂贵的)......而大多数调用BufferedInputStream.read()
都会从缓冲区返回数据。简而言之,如果您正在进行“小”读取,BufferedInputStream
则将a放入流堆栈将提高性能。
For most purposes / use-cases, that is all that is relevant.
There are a few other things (like mark / reset / skip) but these are rather specialist ...
For more detailed information, read the javadocs... and the source code.
对于大多数目的/用例,这就是相关的全部。
还有一些其他的东西(比如标记/重置/跳过)但这些是相当专业的......
有关更多详细信息,请阅读javadocs... 和源代码。
1 - Or more precisely, from some object that 1) has a name in the operating system's "file system" namespace, and 2) that the operating system allows you to read as a sequence of bytes. This mayencompass devices, named pipes, and various other things that one might not consider as "files". It is also worth noting that there some kinds of things that definitely cannotbe read using a FileInputStream
.
1 - 或者更准确地说,从某个对象 1) 在操作系统的“文件系统”命名空间中具有名称,以及 2) 操作系统允许您作为字节序列读取。这可能包括设备、命名管道和其他各种可能不被视为“文件”的东西。还值得注意的是,有些东西绝对不能使用FileInputStream
.
回答by Rajendra arora
You must googlefor that or read Javadocs,
public class FileInputStream
extends InputStream
A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment.
FileInputStream 从文件系统中的文件中获取输入字节。哪些文件可用取决于主机环境。
FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.
FileInputStream 用于读取原始字节流,例如图像数据。要读取字符流,请考虑使用 FileReader。
For more details: https://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html.
有关更多详细信息:https: //docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html。
public class BufferedInputStream
extends FilterInputStream
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.
BufferedInputStream 向另一个输入流添加功能,即缓冲输入和支持标记和重置方法的能力。创建 BufferedInputStream 时,会创建一个内部缓冲区数组。当读取或跳过流中的字节时,内部缓冲区会根据需要从包含的输入流中重新填充,一次很多字节。标记操作会记住输入流中的一个点,重置操作会导致在从包含的输入流中获取新字节之前重新读取自最近的标记操作以来读取的所有字节。
For more details https://docs.oracle.com/javase/7/docs/api/java/io/BufferedInputStream.html.
有关更多详细信息,请访问 https://docs.oracle.com/javase/7/docs/api/java/io/BufferedInputStream.html。
回答by Chris Martin
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.