SORU
14 Mayıs 2010, Cuma


Java seri hale getirilebilir Dizi Bayt Nesne

Java Seri hale getirme için aramalarım örnekler belgenin En bir dosyaya yazma veya okuma.

benim sorum seri hale getirilebilir bir sınıf AppMessage var diyelim.

Bayt alınan yeniden başka bir makine için yuva byte[] olarak iletmek istiyorum.

nasıl elde miyim?

şimdiden anlayışın için teşekkürler.

CEVAP
14 Mayıs 2010, Cuma


Bayt göndermek için hazırlayın

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
try {
  out = new ObjectOutputStream(bos);   
  out.writeObject(yourObject);
  byte[] yourBytes = bos.toByteArray();
  ...
} finally {
  try {
    if (out != null) {
      out.close();
    }
  } catch (IOException ex) {
    // ignore close exception
  }
  try {
    bos.close();
  } catch (IOException ex) {
    // ignore close exception
  }
}

Bayt: nesne oluşturun

ByteArrayInputStream bis = new ByteArrayInputStream(yourBytes);
ObjectInput in = null;
try {
  in = new ObjectInputStream(bis);
  Object o = in.readObject(); 
  ...
} finally {
  try {
    bis.close();
  } catch (IOException ex) {
    // ignore close exception
  }
  try {
    if (in != null) {
      in.close();
    }
  } catch (IOException ex) {
    // ignore close exception
  }
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • androidandme

    androidandme

    10 Mart 2009
  • FF Radio

    FF Radio

    16 Mayıs 2008
  • The Brister

    The Brister

    10 ŞUBAT 2008