java:在运行时跟踪文件大小?

发布于 2024-11-06 23:36:51 字数 904 浏览 6 评论 0原文

我编写了一个将压缩对象写入文件的代码,我的问题是:有没有一种方法可以跟踪写入对象时文件大小的增量?这是我的代码:

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

我尝试在每个 oos.writeObject(str); 之后使用 flush ,然后使用 outFile.length()< 获取文件大小/code>,但无论我如何刷新它,文件大小都保持不变,直到最后跳转到其最终大小。无论如何我可以修复它吗?谢谢

I wrote a code that writes compressed objects into a file, My questions is: is there a way that I could keep track of the increment of size of my file as the object being wrote in? here is my code:

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

I tried to use flush after every oos.writeObject(str); and then get the file size by using outFile.length(), but no matter how much I flush it, the file size remain unchanged until the last jump to its final size. Anyway that I could fix it? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

無處可尋 2024-11-13 23:36:51

Apache Commons 项目提供了一个类 CountingOutputStream,您可以将其放入 OutputStream 链中。您甚至可以拥有其中两个:

package so5997784;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.zip.GZIPOutputStream;

import org.apache.commons.io.output.CountingOutputStream;

public class CountBytes {

  private static void dump(File outFile, Object... objs) throws IOException {
    FileOutputStream fos = new FileOutputStream(outFile);
    try {
      CountingOutputStream compressedCounter = new CountingOutputStream(fos);
      OutputStream gz = new GZIPOutputStream(compressedCounter);
      CountingOutputStream uncompressedCounter = new CountingOutputStream(gz);
      ObjectOutputStream oos = new ObjectOutputStream(uncompressedCounter);

      for (Object obj : objs) {
        oos.writeObject(obj);
        oos.flush();
        System.out.println(uncompressedCounter.getByteCount() + " -> " + compressedCounter.getByteCount());
      }
      oos.close();
      System.out.println(uncompressedCounter.getByteCount() + " -> " + compressedCounter.getByteCount());

    } finally {
      fos.close();
    }
  }

  public static void main(String[] args) throws IOException {
    File outFile = new File("objects.out.gz");
    dump(outFile, "a", "b", "cde", "hello", "world");
  }

}

The Apache Commons project provides a class CountingOutputStream, which you can put into your chain of OutputStreams. You can even have two of them:

package so5997784;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.zip.GZIPOutputStream;

import org.apache.commons.io.output.CountingOutputStream;

public class CountBytes {

  private static void dump(File outFile, Object... objs) throws IOException {
    FileOutputStream fos = new FileOutputStream(outFile);
    try {
      CountingOutputStream compressedCounter = new CountingOutputStream(fos);
      OutputStream gz = new GZIPOutputStream(compressedCounter);
      CountingOutputStream uncompressedCounter = new CountingOutputStream(gz);
      ObjectOutputStream oos = new ObjectOutputStream(uncompressedCounter);

      for (Object obj : objs) {
        oos.writeObject(obj);
        oos.flush();
        System.out.println(uncompressedCounter.getByteCount() + " -> " + compressedCounter.getByteCount());
      }
      oos.close();
      System.out.println(uncompressedCounter.getByteCount() + " -> " + compressedCounter.getByteCount());

    } finally {
      fos.close();
    }
  }

  public static void main(String[] args) throws IOException {
    File outFile = new File("objects.out.gz");
    dump(outFile, "a", "b", "cde", "hello", "world");
  }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文