java中`InputStream``DataInputStream`和`BufferedInputStream`的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9805192/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:50:35  来源:igfitidea点击:

The difference of `InputStream` `DataInputStream` and `BufferedInputStream` in java?

java

提问by alan.chen

The difference of InputStreamDataInputStreamand BufferedInputStreamin java?

InputStreamDataInputStreamBufferedInputStream在java中的区别?

采纳答案by Snicolas

If the question is : "what the difference between those classes", here is a summary, but read the javadoc for more info :

如果问题是:“这些类之间有什么区别”,这里是一个摘要,但请阅读 javadoc 以获取更多信息:

An inputStream is the base class to read bytes from a stream (network or file). It provides the ability to read bytes from the stream and detect the end of the stream.

inputStream 是从流(网络或文件)读取字节的基类。它提供了从流中读取字节并检测流结束的能力。

DataInputStream is a kind of InputStream to read data directly as primitive data types.

DataInputStream 是一种 InputStream 直接读取数据作为原始数据类型。

BufferedInputStream is a kind of inputStream that reads data from a stream and uses a buffer to optimize speed access to data. data is basicaly read ahead of time and this reduces disk or network access.

BufferedInputStream 是一种 inputStream,它从流中读取数据并使用缓冲区来优化对数据的访问速度。数据基本上是提前读取的,这减少了磁盘或网络访问。

回答by Robert

You can test for different InputStreamimplementations using the instanceofoperator:

您可以InputStream使用instanceof运算符测试不同的实现:

InputStream in = ...
if (in instanceof DataInputStream) {
  // we have an DataInputStream instance
} else if (in instanceof BufferedInputStream) {
  // we have an BufferedInputStream instance
}

回答by Raki

I think you are asking difference so i am giving some details

我想你是在问不同,所以我提供了一些细节

Input Stream means:this abstract class is the superclass of all classes representing an input stream of bytes.Applications that need to define a subclass of InputStream must always provide a method that returns the next byte of input.

Input Stream means:这个抽象类是所有表示字节输入流的类的超类。需要定义 InputStream 子类的应用程序必须始终提供一个方法来返回输入的下一个字节。

DataInputStream:A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way. An application uses a data output stream to write data that can later be read by a data input stream.

DataInputStream:数据输入流允许应用程序以独立于机器的方式从底层输入流读取原始 Java 数据类型。应用程序使用数据输出流来写入稍后可由数据输入流读取的数据。

For More use this link http://docs.oracle.com/javase/6/docs/api/java/io/DataInputStream.html

更多使用此链接http://docs.oracle.com/javase/6/docs/api/java/io/DataInputStream.html

BufferedInputStream: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.

BufferedInputStream:A BufferedInputStream 向另一个输入流添加功能,即缓冲输入和支持标记和重置方法的能力。创建 BufferedInputStream 时,会创建一个内部缓冲区数组。当读取或跳过流中的字节时,内部缓冲区会根据需要从包含的输入流中重新填充,一次很多字节。