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 字符。我不明白你能帮帮我吗。