Oh. Well for starters, the number of bytes occupied by a serialized object in a file is not necessarily the same as the number of bytes it would occupy in memory. So Roseanne and I were answering a different question than the one you need. Offhand I'd say your best bet would be to count the bytes as they are written to an ObjectOutputStream. You can use a CheckedOutputStream to do this easily - create a Checksum class which simply counts bytes:
<code><pre>
import java.io.*;
import java.util.zip.*;
class ByteCounter implements Checksum {
private long count;
public long getValue() {
return count;
}
public void reset() {
count = 0;
}
public void update(byte[] b, int off, int len) {
count += len;
}
public void update(int b) {
count++;
}
}
public class
Test {
public final static void main(String[] s) throws Exception
{
File file = new File("objects.ser");
OutputStream os = new BufferedOutputStream(
new FileOutputStream(file));
Checksum counter = new ByteCounter();
CheckedOutputStream cos = new CheckedOutputStream(os, counter);
ObjectOutputStream oos = new ObjectOutputStream(cos);
oos.writeObject("Testing...");
oos.flush();
System.out.println(counter.getValue());
oos.writeObject(new Double(1234.5));
oos.flush();
System.out.println(counter.getValue());
oos.writeObject(new int[1000]);
oos.flush();
System.out.println(counter.getValue());
oos.writeObject("another");
oos.flush();
System.out.println(counter.getValue());
oos.close();
}
}
</pre></code>
I'll leave the rest to you...
[This message has been edited by Jim Yingst (edited February 23, 2001).]