Java - 字节[]到byte []

int*_*_32 16 java byte bytearray

有Vector和DataOutputStream.我需要将Vector中的字节(toArray返回Byte [])写入流,但它只能理解byte [].如何将Byte []转换为byte []?

tim*_*tes 28

您可以在Apache Commons langArrayUtils类中使用toPrimitive方法吗?


jac*_*des 9

byte[] toPrimitives(Byte[] oBytes)
{
    byte[] bytes = new byte[oBytes.length];

    for(int i = 0; i < oBytes.length; i++) {
        bytes[i] = oBytes[i];
    }

    return bytes;
}
Run Code Online (Sandbox Code Playgroud)

逆:

// byte[] to Byte[]
Byte[] toObjects(byte[] bytesPrim) {
    Byte[] bytes = new Byte[bytesPrim.length];

    int i = 0;
    for (byte b : bytesPrim) bytes[i++] = b; // Autoboxing

    return bytes;
}
Run Code Online (Sandbox Code Playgroud)

freeone3000在这个答案中做出了贡献:)