Java ObjectOutputStream 未写入 ZipEntry
Java ObjectOutputStream Not Writing to ZipEntry
我正在尝试使用 ObjectOutputStream 将对象序列化为 ZipEntry,但它似乎没有写入任何内容,因为当我打印生成的字节数组时,它显示为空。我尝试使用 ZipOutputStream 编写一个字符串,并且在打印生成的字节数组时得到了一个相当大的结果。所以我的问题是:为什么对象输出流没有正确写入 ZipEntry。 (ConfigEntry 确实实现了 Serializable)。
String s = "Tired, Exhausted";
ConfigEntry con = new ConfigEntry("rand", "random", 3);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ZipOutputStream zos = new ZipOutputStream(baos);
ZipEntry entry = new ZipEntry("test.txt");
ObjectOutputStream obs = new ObjectOutputStream(zos);
zos.putNextEntry(entry);
obs.writeObject(con);
obs.close();
zos.closeEntry();
zos.close();
} catch(IOException ioe) {
ioe.printStackTrace();
}
os = bs.getOutputStream();
byte[] result = baos.toByteArray();
String test = new String(result, "UTF-8");
Log.v("Mac Address", test);
Log.v("Mac Address", Arrays.toString(result));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
此 baos
在 try
块之后超出范围。您正在写入一个 baos
并且正在查看在外部范围内声明的另一个 baos
,可能是 class.
的实例成员
我正在尝试使用 ObjectOutputStream 将对象序列化为 ZipEntry,但它似乎没有写入任何内容,因为当我打印生成的字节数组时,它显示为空。我尝试使用 ZipOutputStream 编写一个字符串,并且在打印生成的字节数组时得到了一个相当大的结果。所以我的问题是:为什么对象输出流没有正确写入 ZipEntry。 (ConfigEntry 确实实现了 Serializable)。
String s = "Tired, Exhausted";
ConfigEntry con = new ConfigEntry("rand", "random", 3);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ZipOutputStream zos = new ZipOutputStream(baos);
ZipEntry entry = new ZipEntry("test.txt");
ObjectOutputStream obs = new ObjectOutputStream(zos);
zos.putNextEntry(entry);
obs.writeObject(con);
obs.close();
zos.closeEntry();
zos.close();
} catch(IOException ioe) {
ioe.printStackTrace();
}
os = bs.getOutputStream();
byte[] result = baos.toByteArray();
String test = new String(result, "UTF-8");
Log.v("Mac Address", test);
Log.v("Mac Address", Arrays.toString(result));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
此 baos
在 try
块之后超出范围。您正在写入一个 baos
并且正在查看在外部范围内声明的另一个 baos
,可能是 class.