Java:对象输入流
public void bar(String fileName) throws IOException{
FileInputStream fileIn = new FileInputStream(fileName);
ObjectInputStream in = new ObjectInputStream(fileIn);
Map map = (HashMap) in.readObject();
}
我试图理解这段代码的作用。
我们创建一个流,这样我们就可以读取该文件。这个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? Do we read object and make a map out of it? I clearly don't understand, and I'll be glad for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ObjectInputStream
将读取由ObjectOutputStream
在文件中序列化的对象,此代码将从
fileName
创建 InputStream(字符串,文件的绝对路径)HashMap
对象,保存到Map map
变量中这意味着,在文件中,有一个
HashMap
类型的对象它将被投射到使用此代码的Map
ObjectInputStream
will read Object serialized in file byObjectOutputStream
this code will
fileName
(String, absolute path to file)HashMap
object, saved toMap map
variableSo that mean, in file, there is a object of type
HashMap
which will be casted toMap
with this code我的猜测是它正在读取之前使用相应的
Out
/Write
方法写入文件的HashMap
。My guess is it is reading a
HashMap
that was previously written to the file using correspondingOut
/Write
methods.