Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you have access to 3rd party libraries, just use XStream to serialize to XML. If not, you can serialize and save.</p> <p>Follow a serialization tutorial like <a href="http://www.tutorialspoint.com/java/java_serialization.htm" rel="nofollow">this: http://www.tutorialspoint.com/java/java_serialization.htm</a></p> <p>I don't think you want to try to remove anything from the file.. just rewrite it when you make a change. Make a couple methods to read in the file and also to serialize objects and save to file. Here is an example</p> <p>Group.java</p> <pre><code>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; } } </code></pre> <p>GroupData.java</p> <pre><code>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&lt;Group&gt; { 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; } } } </code></pre> <p>Here is a test </p> <p>TestGroup.java</p> <pre><code>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); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload