0
public static byte[] objectToByteArray(Object obj) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream objOut = null;
try {
    objOut = new ObjectOutputStream(out);
    objOut.writeObject(obj);
    objOut.flush();
} finally {
    objOut.close();
    out.close();
}
return out.toByteArray();
}

Main Method :



 public static void main(String[] args) throws IOException {
//
// System.out.println(getFileName("/home/local/ZOHOCORP/bharathi-1397/logs/bharathi-1397.csez.zohocorpin.com_2012_05_24.log",0));
try {
    throw new IOException("Error in main method");
} catch (IOException io) {
    System.out.println(new String(objectToByteArray(io.getMessage()),
        "UTF-8"));
}
//
}

出力 : ��

Object を byte[] に変換したいのですが、なぜこのような ctrl 文字を返すのですか? わかりませんでした。助けてください。

4

4 に答える 4

1

Serialization converts an object into binary data. It's fundamentally not text data - just like an image file isn't text data.

Your code tries to interpret this opaque binary data as if it were UTF-8-encoded text data. It's not, so no wonder you're seeing garbage. If you opened up an image file in a text editor, you'd see similar non-useful text. You can try to interpret the data as text (as you are doing) but you won't get anything useful out.

If you want to represent opaque binary data as text in a printable, reversible manner you should use base64 or hex. There are lots of libraries available to convert to base64, including this public domain one.

于 2012-05-24T06:03:53.127 に答える
1

Java store the object in its own binary format when you use ObjectOutputStream. You have to use ObjectInputStream to decode data into proper Object before use.

于 2012-05-24T06:03:59.237 に答える
0

java api doc: public final void writeObject(Object obj) が IOException をスローします。指定されたオブジェクトを ObjectOutputStream に書き込みます。オブジェクトのクラス、クラスの署名、およびクラスの非一時的および非静的フィールドの値とそのすべてのスーパータイプが書き込まれます。クラスのデフォルトのシリアル化は、writeObject および readObject メソッドを使用してオーバーライドできます。このオブジェクトによって参照されるオブジェクトは推移的に書き込まれるため、オブジェクトの完全に同等のグラフを ObjectInputStream によって再構築できます。

http://docs.oracle.com/javase/1.4.2/docs/api/java/io/ObjectOutputStream.html#writeObject(java.lang.Object )

于 2012-05-24T06:10:25.427 に答える
0

ファイルとの間でバイト値を読み書きするための以下のアプローチを見つけることができます。

16 進文字列に変換してから Base64 形式に変換します

Base64 形式のファイルを読み取るには

    byte [] encDataByteArr = hexToBytes(encData);
    byte [] dataBack = Base64.decodeBase64(encDataByteArr);

ファイルに書き込む

    byte [] data = Base64.encodeBase64(encDataByteArr);
    returnValue = bytesToHex(data);

そのためのいくつかのユーティリティメソッドは以下のとおりです

public static synchronized String bytesToHex(byte [] buf){
    StringBuffer strbuf = new StringBuffer(buf.length * 2);
    int i;
    for (i = 0; i < buf.length; i++) {
        if (((int) buf[i] & 0xff) < 0x10){
            strbuf.append("0");
        }
        strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
    }
    return strbuf.toString();
}

public synchronized static byte[] hexToBytes(String hexString) {
     byte[] b = new BigInteger(hexString,16).toByteArray();     
     return b;
}
于 2012-05-24T06:35:35.957 に答える