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 字符 。 我无法理解您能否帮助我 。