Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在不使用Hibernate的情况下将数据保存到Java文件中?_Java_Hibernate - Fatal编程技术网

如何在不使用Hibernate的情况下将数据保存到Java文件中?

如何在不使用Hibernate的情况下将数据保存到Java文件中?,java,hibernate,Java,Hibernate,如何将我的收藏“数据库”保存到一个文件中,然后在不使用Hibernate的情况下读取它 我用以下代码保存它: ObjectOutputStream save = null; try{ save = new ObjectOutputStream(new FileOutputStream("data.txt")); save.writeObject(database); return true; }catch(IOException e){ System.err.p

如何将我的收藏“数据库”保存到一个文件中,然后在不使用Hibernate的情况下读取它

我用以下代码保存它:

ObjectOutputStream save = null;

try{
    save = new ObjectOutputStream(new FileOutputStream("data.txt"));
    save.writeObject(database);
    return true;
}catch(IOException e){
    System.err.println("I/O error!");
    return false;
}finally{
    save.close();
}
File file = new File("data.txt");
if(!(file.exists()))
    save();
ObjectInputStream read = null;

try{
    read = new ObjectInputStream(new FileInputStream("data.txt"));
    database =  (Set<Allincomes>) read.readObject();
    return true;
}catch(IOException e){
    System.err.println("I/O error!");
    return false;
}catch(ClassNotFoundException e){
    System.err.println("Class not found!");
    return false;
}finally{
    read.close();
}
然后我用以下代码读取文件:

ObjectOutputStream save = null;

try{
    save = new ObjectOutputStream(new FileOutputStream("data.txt"));
    save.writeObject(database);
    return true;
}catch(IOException e){
    System.err.println("I/O error!");
    return false;
}finally{
    save.close();
}
File file = new File("data.txt");
if(!(file.exists()))
    save();
ObjectInputStream read = null;

try{
    read = new ObjectInputStream(new FileInputStream("data.txt"));
    database =  (Set<Allincomes>) read.readObject();
    return true;
}catch(IOException e){
    System.err.println("I/O error!");
    return false;
}catch(ClassNotFoundException e){
    System.err.println("Class not found!");
    return false;
}finally{
    read.close();
}
File File=new文件(“data.txt”);
如果(!(file.exists()))
save();
ObjectInputStream读取=null;
试一试{
read=newobjectinputstream(newfileinputstream(“data.txt”);
数据库=(设置)read.readObject();
返回true;
}捕获(IOE异常){
System.err.println(“I/O错误!”);
返回false;
}catch(classnotfounde异常){
System.err.println(“找不到类!”);
返回false;
}最后{
read.close();
}

但是我的老师说不要使用Hibernate,因为我保存所有数据的文件正如他所说的“不可读”。

你的代码中没有Hibernate。我个人会避免序列化你的对象,因为它只用于短期存储。要么编写自己的例程,要么根据对象的类型编写内容,要么考虑使用java之类的东西,甚至JSON(查找GSON),这将为您提供一种手段,使您可以在更稳定的状态下将对象序列化以进行长期存储。