Java tutorial
import java.io.File; import java.io.FileOutputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.zip.GZIPOutputStream; public class Main { public static void main(String[] args) throws Exception { User admin = new User(); admin.setId(new Long(1)); User foo = new User(); foo.setId(new Long(2)); ObjectOutputStream oos = new ObjectOutputStream( new GZIPOutputStream(new FileOutputStream(new File("user.dat")))); oos.writeObject(admin); oos.writeObject(foo); oos.flush(); oos.close(); } } class User implements Serializable { private Long id; public User() { } public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("id=").append(id); return sb.toString(); } }