java持久化对象到文件后怎么读取出来?
3个回答
展开全部
public static Object readObjectFromFile()
{
Object temp=null;
File file =new File("test.dat");
FileInputStream in;
try {
in = new FileInputStream(file);
ObjectInputStream objIn=new ObjectInputStream(in);
temp=objIn.readObject();
objIn.close();
System.out.println("read object success!");
} catch (IOException e) {
System.out.println("read object failed");
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return temp;
}
{
Object temp=null;
File file =new File("test.dat");
FileInputStream in;
try {
in = new FileInputStream(file);
ObjectInputStream objIn=new ObjectInputStream(in);
temp=objIn.readObject();
objIn.close();
System.out.println("read object success!");
} catch (IOException e) {
System.out.println("read object failed");
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return temp;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询