示例代码:
ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); dos.writeLong(number);
byte[] bytes = bos.toByteArray(); ByteArrayInputStream bis = new ByteArrayInputStream(bytes); DataInputStream dis = new DataInputStream(bis); long readLong = dis.readLong();
示例代码:
ByteArrayOutputStream bos = new ByteArrayOutputStream(); ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES); buffer.putLong(number); bos.write(buffer.array());
byte[] bytes = bos.toByteArray(); ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
byte[] longBytes = new byte[Long.BYTES]; bis.read(longBytes, 0, Long.BYTES); long readLong = BitConverter.toInt64(longBytes, 0);
注意:使用第二种方法需要在代码中添加BitConverter类的实现,或使用Java的BigInteger类进行转换。
上一篇:ByteArrayInputStream无法初始化ObjectInputStream。
下一篇:ByteArrayInputStream无法读取由ByteArrayOutputStream写入的long类型数据。