Created
December 22, 2017 05:34
-
-
Save rashemihmih/be4da921c29afe24a6d14e9a4978e2e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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