


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
How to use java serialization to convert objects into byte sequences and back, enabling data transfer and lightweight persistence. It covers the serializable and externalizable interfaces, serialization process, and handling of connected objects. Examples are provided.
Typology: Exercises
1 / 4
This page cannot be seen from the preview
Don't miss anything!



import java.io.*;
public class Data implements Serializable { public static void main(String[] args) throws ClassNotFoundException, IOException { Data d = new Data(); ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("data.out")); out.writeObject("Data storage"); out.writeObject(d); out.close();
ObjectInputStream in = new ObjectInputStream( new FileInputStream("data.out"));
String s = (String)in.readObject(); Data d2 = (Data)in.readObject(); } }
import java.io.*;
public class GetAlienClass { public static void main(String[] args) throws IOException, ClassNotFoundException { ObjectInputStream in = new ObjectInputStream( new FileInputStream("alien.obj")); Object obj = in.readObject();
// Here you will get ClassNotFoundException System.out.println(obj.getClass()); } }
import java.io.; import java.util.;
class LoginCredentials implements Serializable {
private String username; private transient String password;
LoginCredentials(String name, String password) { username = name; this.password = password; }
public static void main(String[] args) throws IOException, ClassNotFoundException { LoginCredentials = new LoginCredentials("peter","mikhalenko"); } }