title: Serialize/Deserialize Java object through network and byte array
body: <p>I have this code from DZone(<a href="http://www.dzone.com/links/r/java_custom_serialization_example.html" rel="noreferrer">http://www.dzone.com/links/r/java_custom_serialization_example.html</a>) that serialize/deserialize Java object from/to file. </p>
  
  <pre><code>final class Hello implements Serializable
  {
      int x = 10;
      int y = 20;
  
      public int getX()
      {
          return x;
      }
      public int getY()
      {
          return y;
      }
  }
  
  
  public class SerializedComTest {
  
      @AfterClass
      public static void tearDownAfterClass() throws Exception {
      }
  
      @Test
      public void testFile() throws IOException, ClassNotFoundException {
          Hello h = new Hello();
          FileOutputStream bs = new FileOutputStream("hello.txt"); // ("testfile");
          ObjectOutputStream out = new ObjectOutputStream(bs);
          out.writeObject(h);
          out.flush();
          out.close();
  
          Hello h2;
          FileInputStream fis = new FileInputStream("hello.txt");
          ObjectInputStream ois = new ObjectInputStream(fis);
          h2 = (Hello) ois.readObject();
  
          assertTrue(10 == h2.getX());
          assertTrue(20 == h2.getY());
      }
  }
  </code></pre>
  
  <p>How can I transfer serialized object using Java socket? And also how can I store the serialized/deserialized object to/from a byte array.</p>