English 中文(简体)
为什么反对在 java 中返回 jumbled 字符?
原标题:why object to byte[] returns jumbled characters in java?
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"));
}
//
}

Output : ��

我想将对象转换为字节, 但为什么它返回像这样的 ctrl 字符 。 我无法理解您能否帮助我 。

最佳回答

序列化将对象转换为二进制数据。它基本上是not 文本数据 - 就像图像文件不是文本数据一样。

您的代码试图 解释 这个不透明的二进制数据, 仿佛它是 UTF-8- encoded 文本数据。 它不是, 所以难怪您会看到垃圾 。 如果您在文本编辑器中打开了一个图像文件, 您就会看到类似的不有用的文本 。 您可以 < em> try 将数据解释成文本( 正如您所做的那样), 但是您不会得到任何有用的信息 。

如果您想要以可打印、可逆的方式将不透明的二进制数据作为文本表示, 您应该使用 base64 或 hex。 有许多可转换到 base64 的图书馆, 包括 < a href=" "http:// iharder. net/ base64" rel= “ nofollow” > this public域 one

问题回答

Java 在使用 ObjectPutStream 时, 以自己的二进制格式存储对象。 您必须使用 ObjectInputStream 在使用前将数据解码到合适的对象 。

java api doc: public final void writeObject(Object obj) throws IOException; Write the specified object to the ObjectOutputStream. The class of the object, the signature of the class, and the values of the non-transient and non-static fields of the class and all of its supertypes are written. Default serialization for a class can be overridden using the writeObject and the readObject methods. Objects referenced by this object are written transitively so that a complete equivalent graph of objects can be reconstructed by an ObjectInputStream.

< a href=>http://docs.oracle.com/javase/1.4.2/docs/api/java/io/Object OutputStream.html#writeObject(java.lang.Object) rel=“nofollow”>http://docs.oracle.com/javase/1.4.2/docs/api/java/io/Object OpjectStream.html#writeObject(java.lang.Object) >

您可以找到以下写入和读取字节值的方法。

转换为 Hex 字符串, 然后转换为 Base64 格式

To Read the file Base64 Format

    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;
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签