【发布时间】:2015-01-09 17:59:23
【问题描述】:
下面是我用来从文件中读取和写入 HashMap 的代码。我想知道,无论如何我可以重新分解这段代码,以便可以读入和写出任何数据结构吗?而不是为每个数据结构创建一个新类?感谢您提供的任何帮助!
public class FileHandler {
static ObjectOutputStream oos;
static ObjectInputStream ois;
public static void writeOut(HashMap p, File selection) throws FileNotFoundException, IOException
{
oos = new ObjectOutputStream(new FileOutputStream(selection));
oos.writeObject(p);
oos.close();
}
public static HashMap<String, Object> readIn(File selection) throws FileNotFoundException, IOException, ClassNotFoundException
{
HashMap<String, Object> temp = null;
ois = new ObjectInputStream(new FileInputStream(selection));
temp = (HashMap<String, Object>) ois.readObject();
ois.close();
return temp;
}
}
【问题讨论】:
标签: java file data-structures objectinputstream objectoutputstream