This may be a silly one, but I want to know the background operation difference.
InputStream is = new FileInputStream(filepath);
FileIn
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.