This is not an easy problem. To start with, you need to know exactly what you want to measure. For a BufferedImage
, you need to specify which TYPE because the number of bytes needed per pixel depends on it.
For excel spread sheets and XML files, you need to specify which library you want to use.
Event then, the only way to know is by measuring. You can take several measurements and extrapolate - this is not guaranteed to be exactly correct but will yield useful estimates.
These posts show how you can do that:
The last post, "In Java, what is the best way to determine the size of an object?", only measures the size of the object itself, not the object graph that it creates.
This external link titled Again about determining size of Java object has an extension of the solution in the last post, which estimates the size of an object graph. It's too long to reproduce here (and it's not my code to copy) but the link has existed since 2007 so I believe it's quite stable.