0

我希望能够将在我的程序中创建的对象保存到文件中。我看过一些关于 ObjectOutputStream 的教程,但问题是,它们只展示了如何保存在 main 方法中创建的特定对象。我想要的是,程序会自动保存每个创建的对象。以我程序中的 Group Object 为例。这是添加方法:

public void addGroup(int gid, String groupname) {
    Group newgroup = new Group(gid, groupname);
    if (!Groups.contains(newgroup)) {
        Groups.add(newgroup);
        return;
    }else
        JOptionPane.showMessageDialog(null, "Group with ID " + gid
                + " already exists!", "Error",
                JOptionPane.WARNING_MESSAGE);
}

它是我的数据库类的一部分。我想自动将每个创建的组保存到文件中。这将如何完成?在数据库类中,我在哪里声明新文件?在主要方法中?

我的第二个问题是,如果我删除一个组,使用 remove 方法:

public void removeGroup(int gid) {

    if (!Groups.remove(new Group(gid, null))) {
        JOptionPane.showMessageDialog(null, "Group with ID[" + gid
                + "] not present. System unchanged.");
    }

}

如何从文件中删除它?我知道,我不能真正从文件中删除一个对象,但是我将如何清空这个空间?

提前感谢所有帮助:)

4

2 回答 2

2

如果您有权访问 3rd 方库,只需使用 XStream 序列化为 XML。如果没有,您可以序列化并保存。

遵循这样的序列化教程:http://www.tutorialspoint.com/java/java_serialization.htm

我认为您不想尝试从文件中删除任何内容..只需在进行更改时重写它即可。制作几个方法来读取文件,并序列化对象并保存到文件。这是一个例子

组.java

import java.io.Serializable;

public class Group implements Serializable
{
/**
 * 
 */
private static final long serialVersionUID = 1L;

private String guid;
private String groupName;

public Group(String guid, String groupName) {
    super();
    this.guid = guid;
    this.groupName = groupName;
}

public String getGuid() {
    return guid;
}
public void setGuid(String guid) {
    this.guid = guid;
}
public String getGroupName() {
    return groupName;
}
public void setGroupName(String groupName) {
    this.groupName = groupName;
}
}

组数据.java

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class GroupData extends ArrayList<Group>
{
private static final long serialVersionUID = 1L;

public GroupData(){}

public void addGroup(Group group)
{
    this.add(group);
    saveGroupData();
}

public void removeGroup(Group group)
{
    this.remove(group);
    saveGroupData();
}

public void saveGroupData()
{
      try
      {
         FileOutputStream fileOut = new FileOutputStream("C:\\Users\\tester\\group-data.ser");
         ObjectOutputStream out = new ObjectOutputStream(fileOut);

         out.writeObject(this);

         out.close();
         fileOut.close();
      }
      catch(IOException i)
      {
          i.printStackTrace();
      }
}

public void loadGroupData()
{
      try
      {
         FileInputStream fileIn = new FileInputStream("C:\\Users\\tester\\group-data.ser");
         ObjectInputStream in = new ObjectInputStream(fileIn);
         GroupData tmp = (GroupData) in.readObject();

         this.clear();
         this.addAll(tmp);

         in.close();
         fileIn.close();
      }
      catch(IOException i)
      {
         i.printStackTrace();
         return;
      }
      catch(ClassNotFoundException c)
      {
         c.printStackTrace();
         return;
      }
}
}

这是一个测试

测试组.java

public class TestGroup {

/**
 * @param args
 */
public static void main(String[] args) 
{

    Group group1 = new Group("123", "testers");
    Group group2 = new Group("456", "programmers");
    Group group3 = new Group("687", "students");

    GroupData groupData = new GroupData();

    groupData.add(group1);
    groupData.add(group2);
    groupData.add(group3);

    groupData.remove(group3);
}

}
于 2013-04-11T21:58:30.270 回答
0

This question can be answered in a number of ways. There are libraries out there for serialization like XStream that help you to serialize your objects. There area also libraries that will help you manage persistence (i.e. writing objects to file and being able to read them back into objects).

For something simple, you might just want to write a simple text file, or you might want that file to be binary.

Or you might just want to record the fact that objects were created and include some basic information about them from the objects themselves. For that, you want to look at one of the available log libraries, like slf4j, java.util.logging, or log4j

As far as deleting things from a file goes, you're essentially reading a file and replacing it without the part you want to erase. This page has some code that outlines a strategy for accomplishing that.

于 2013-04-11T22:19:20.430 回答