What is the difference between FileInputStream and BufferedInputStream in Java?

What is the difference between FileInputStream and BufferedInputStream in Java?

+3
source share
3 answers

The main differences:

  • BufferedInputStreambuffered, but FileInputStreamnot.

  • A BufferedInputStreamis read from another InputStream, but a is FileInputStreamreading from file 1 .

In practice, this means that every call FileInputStream.read()will make a system call (costly) ... while most calls BufferedInputStream.read()will return data from the buffer. In short, if you are doing small reads, adding BufferedInputStreama stream to the stack will improve performance.

  • For most purposes / use cases, this is all that matters.

  • There are a few more things (e.g. mark / reset / skip), but this is more of a specialist ...

  • javadocs... .


1 - , , - , 1) " " 2), . , , "". , , FileInputStream.

+12

google Javadocs,

public class FileInputStream
extends InputStream

FileInputStream . .

FileInputStream , . FileReader.

: https://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html.

public class BufferedInputStream
extends FilterInputStream

BufferedInputStream , : reset. BufferedInputStream , . , , . , reset , , , .

https://docs.oracle.com/javase/7/docs/api/java/io/BufferedInputStream.html.

+1
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.
+1
source

All Articles