开发者

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

  1. create InputStream from fileName (String, absolute path to file)
  2. create ObjectInputStream, to read objects saved in that file
  3. will create HashMap object, saved to Map 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜