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

  • BrandonHarrisWalker

    BrandonHarri

    27 Kasım 2006
  • celebrateubuntu

    celebrateubu

    23 Mayıs 2011
  • Ryan Billy

    Ryan Billy

    30 EKİM 2006