我编写了一个将压缩对象写入文件的代码,我的问题是:有没有一种方法可以跟踪文件大小的增量作为正在写入的对象?这是我的代码:
public static void storeCompressedObjs(File outFile, ArrayList<Object[]> obj) {
FileOutputStream fos = null;
GZIPOutputStream gz = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream(outFile);
gz = new GZIPOutputStream(fos);
oos = new ObjectOutputStream(gz);
for (Object str : obj) {
oos.writeObject(str);
oos.flush();
//I was hoping to print outFile.length() here, but it doesn't work
}
} catch (IOException e) {
e.printStackTrace();
} finally {
oos.close();
gz.close();
fos.close();
}
}
我尝试flush
在 every之后使用oos.writeObject(str);
,然后通过 using 获取文件大小outFile.length()
,但无论我刷新多少,文件大小都保持不变,直到最后一次跳转到最终大小。无论如何我可以解决它?谢谢