When objectinputstream is used to input a stream, an error occurs because a byte [] array is directly used as a parameter.
The Code is as follows:
Package org. test;
Import java. Io. bytearrayinputstream;
Import java. Io. bytearrayoutputstream;
Import java. Io. objectinputstream;
Import java. Io. objectoutputstream;
Import java. util. arraylist;
Import java. util. date;
Public class javatest {
Public static void main (string ARGs []) {
Javatest Jt = new javatest ();
String S = "helloworld! ";
Byte [] bt = NULL;
Bt = S. getbytes ();
Arraylist <string> List = new arraylist <string> ();
List = JT. getarraylist (BT); // This row will have an error.
}
Public arraylist <string> getarraylist (byte [] BT ){
Arraylist <string> List = new arraylist <string> ();
Objectinputstream objips;
// Note that objectinputstream deserializes the basic data and objects previously written using objectoutputstream. The problem is that you directly pass the byte [] array, and this byte array is not written using the objectoutputstream class. The solution is to use the output stream to obtain the byte [] array.
Try {
Objips = new objectinputstream (New bytearrayinputstream (BT ));
List = (arraylist <string>) objips. readobject ();
} Catch (exception e ){
E. printstacktrace ();
}
Return list;
}
Public byte [] getbyte (arraylist <string> List ){
Byte [] bt = NULL;
Bytearrayoutputstream baos = new bytearrayoutputstream ();
Try {
If (list! = NULL)
{
Objectoutputstream objos = new objectoutputstream (baos );
Objos. writeobject (list );
Bt = baos. tobytearray ();
}
} Catch (exception E)
{
Bt = (byte []) NULL;
E. printstacktrace ();
}
Return Bt;
}
}
Modify:
Get byte [] byte array through objectoutputstream. As follows:
Public static void main (string ARGs []) {
Javatest Jt = new javatest ();
String S = "helloworld! ";
Byte [] bt = NULL;
Arraylist <string> List = new arraylist <string> ();
Arraylist <string> byte_list = new arraylist <string> ();
Byte_list.add (s );
Bt = JT. getbyte (byte_list); // call the getbyte () method to obtain the BT [] array.
List = JT. getarraylist (BT );
}