what is the difference in using InputStream instead of FileInputStream while creating the FileInputStream object

て烟熏妆下的殇ゞ 提交于 2019-12-03 07:05:30

问题


This may be a silly one, but I want to know the background operation difference.

  1. InputStream is = new FileInputStream(filepath);
  2. FileInputStream is = new FileInputStream(filepath);

What is the difference between the above two lines of code and in what scenarios are they used.


回答1:


FileInputStream extends InputStream: it is a specialized version of an InputStream designed for reading files.

There are several implementations of an InputStream according to the use of it.

It is usually good practice to use the highest type needed in your code. Therefore if your code needs to read data from an InputStream but not specifically from a FileInputStream, you should use InputStream. Yet if you do need to keep the information of your object being a FileInputStream and not just an InputStream, then you should keep the FileInputStream type.




回答2:


There is no real difference. FileInputStream extends InputStream, and so you can assign an InputStream object to be a FileInputStream object. In the end, it's the same object, so the same operations will happen.

This behavior is called Polymorphism and is very important in Object-Oriented Programming.

Your first line of code is probably more desirable than the second as it doesn't lock you into a FileInputStream.

This is one of the strengths of object oriented programming. Not specifying a type allows you to change what type of stream you are using later on. If you are sure you'll only ever need a FileInputStream here, use the second line of code.




回答3:


The other answers have pretty much nailed it, but I would like to add the following bit.

If the type of the reference variable is is strictly an internal implementation detail of your class, i.e. no other class will ever find out about it, directly or indirectly, then there is really no difference between the two statements, even though I would program against the more basic type (InputStream) just because.

However, if there is even the slightest hint of leaking FileInputStream specific behavior through your class' interface, without this being essential to the problem you are trying to solve, you should always program against the more basic type.

Of course, this is a general good practice and applies to more than InputStreams and the like.




回答4:


Like the other answer state, there is no difference in behaviour. It still is the same object and the same methods will be executed. You can assign an object of any type that inherits InputStream to that variable.

However, what no one mentioned so far is: You can only call operations that are declared in InputStream on that variable. If FileInputStream would offer some additional operations, the compiler would throw an error if you try to call it. In this case you would need to use FileInputStream as type of the variable.




回答5:


There is no difference. In each case you are creating a FileInputStream. The first is probably better programming style in that you should generally use a classes interface instead of the concrete class to allow for flexibility (i.e you decide to use a BufferedInputStream).



来源:https://stackoverflow.com/questions/17531824/what-is-the-difference-in-using-inputstream-instead-of-fileinputstream-while-cre

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!