Skip to content

Instantly share code, notes, and snippets.

@rashemihmih
Created December 22, 2017 05:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rashemihmih/be4da921c29afe24a6d14e9a4978e2e8 to your computer and use it in GitHub Desktop.
Save rashemihmih/be4da921c29afe24a6d14e9a4978e2e8 to your computer and use it in GitHub Desktop.
import java.io.*;
public class Main {
public static void main(String[] args) {
A a = new A(4);
try (OutputStream os = new FileOutputStream("a");
ObjectOutputStream oos = new ObjectOutputStream(os)) {
oos.writeObject(a);
os.write("hello".getBytes());
} catch (IOException e) {
e.printStackTrace();
System.exit(0);
}
try (InputStream is = new FileInputStream("a");
ObjectInputStream ois = new ObjectInputStream(is)) {
A a1 = (A) ois.readObject();
System.out.println(a1.getA());
byte[] buf = new byte[128];
int read = is.read(buf);
System.out.println(new String(buf, 0, read));
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
static class A implements Serializable {
private int a;
public A(int a) {
this.a = a;
}
public int getA() {
return a;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment