public class Person implements Serializable {
private String name;
private int age;
private static final long serialVersionUID = 1L;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public static void main(String[] args) throws FileNotFoundException, IOException{
Person michael = new Person("Michael", 26);
Person peter = new Person("Peter", 37);
FileOutputStream fileOutputStream = new FileOutputStream("persons.txt");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(michael);
objectOutputStream.writeObject(peter);
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter