当前位置: 技术问答>java相关
如何使用socket,在C/S程序之间传递数组?
来源: 互联网 发布时间:2015-03-16
本文导语: 如题,请给出源代码!!谢谢,急用! | public static byte[] bwritedato(Object data) { byte[] bx = new byte[1]; try { ObjectOutputStream fin; ByteA...
如题,请给出源代码!!谢谢,急用!
|
public static byte[] bwritedato(Object data)
{
byte[] bx = new byte[1];
try
{
ObjectOutputStream fin;
ByteArrayOutputStream b = new ByteArrayOutputStream();
fin = new ObjectOutputStream(b);
fin.writeObject(data);
fin.flush();
fin.close();
fin = null;
bx = new byte[b.size()];
bx = b.toByteArray();
}
catch(Exception e)
{
log(" bAppending/writing object data error : " + e.toString());
}
return bx;
}
public static Object breaddato(byte[] data)
{
Object bx = new Object();
try
{
ObjectInputStream fin;
ByteArrayInputStream b = new ByteArrayInputStream(data);
fin = new ObjectInputStream(b);
bx = fin.readObject();
fin.close();
fin = null;
}
catch(Exception e)
{
log(" bAppending/writing object data error : " + e.toString());
}
return bx;
}
{
byte[] bx = new byte[1];
try
{
ObjectOutputStream fin;
ByteArrayOutputStream b = new ByteArrayOutputStream();
fin = new ObjectOutputStream(b);
fin.writeObject(data);
fin.flush();
fin.close();
fin = null;
bx = new byte[b.size()];
bx = b.toByteArray();
}
catch(Exception e)
{
log(" bAppending/writing object data error : " + e.toString());
}
return bx;
}
public static Object breaddato(byte[] data)
{
Object bx = new Object();
try
{
ObjectInputStream fin;
ByteArrayInputStream b = new ByteArrayInputStream(data);
fin = new ObjectInputStream(b);
bx = fin.readObject();
fin.close();
fin = null;
}
catch(Exception e)
{
log(" bAppending/writing object data error : " + e.toString());
}
return bx;
}
|
数组也是Object
用ObjectInputStream.readObject()和ObjectOutputStream.writeObject()
数组元素必须是简单类型或者实现java.io.Serializable接口
用ObjectInputStream.readObject()和ObjectOutputStream.writeObject()
数组元素必须是简单类型或者实现java.io.Serializable接口