Java: ObjectInputStream
public void bar(String fileName) throws IOException{
FileInputStream fileIn = new FileInputStream(fileName);
ObjectInputStream in = new ObjectInputStream(fileIn);
Map map = (HashMap) in.readObject();
}
I'm trying to understand what this piece of code does.
We create a stream, so we'll be able to read from this file. What does this ObjectInputStream
do开发者_JAVA技巧? Do we read object and make a map out of it? I clearly don't understand, and I'll be glad for your help.
ObjectInputStream
will read Object serialized in file by ObjectOutputStream
public void bar(String fileName) throws IOException{
FileInputStream fileIn = new FileInputStream(fileName); //1
ObjectInputStream in = new ObjectInputStream(fileIn); //2
Map map = (HashMap) in.readObject(); //3
}
this code will
- create InputStream from
fileName
(String, absolute path to file) - create ObjectInputStream, to read objects saved in that file
- will create
HashMap
object, saved toMap map
variable
So that mean, in file, there is a object of type HashMap
which will be casted to Map
with this code
My guess is it is reading a HashMap
that was previously written to the file using corresponding Out
/Write
methods.
精彩评论