FileInputStream and ObjectInputStream

I need to read these classes in a file. I really don't understand how they work.

FileInputStream inFile = new FileInputStream(fileName);
ObjectInputStream inStream = new ObjectInputStream(inFile);
car = (Car)inStream.readObject();

If the car is a class, what exactly is read? I am so confused by this.

a car is an instance of the Car class

thank

+3
source share
3 answers

At the most primitive level, you are reading a bit from a file that FileInputStream can do. Then it is filtered through an ObjectInputStream, which translates these bits into Java objects, but does not know the actual type of the created object, so you should discard the object as a car (I hope that there is an error in the try / catch block).

. :
, , :
:

+1

FileInputStream:
FileInputStream is used to open a file for reading.

FileInputStream fis=new FileInputStream("welcome");

It checks whether the file exists or not, if the file exists, opens the file for reading, otherwise a FileNotFoundException will be thrown.

ObjectInputStream:

FileInputStream fis=new FileInputStream("welcome");
ObjectInputStream ois=new ObjectInputStream(fis);

It opens a file for reading an object.

0
source

Source: https://habr.com/ru/post/1785824/


All Articles