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

后端 未结 5 973
天涯浪人
天涯浪人 2021-02-02 13:54

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

  1. InputStream is = new FileInputStream(filepath);
  2. FileIn
5条回答
  •  醉酒成梦
    2021-02-02 14:19

    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.

提交回复
热议问题