Reputation: 61
How to count number of bytes of this binary file (t.dat) without running this code (as a theoretical question) ? Assuming that you run the following program on Windows using the default ASCII encoding.
public class Bin {
public static void main(String[] args){
DataOutputStream output = new DataOutputStream(
new FileOutputStream("t.dat"));
output.writeInt(12345);
output.writeUTF("5678");
output.close();
}
}
Upvotes: 3
Views: 2633
Reputation: 13700
Instead of trying to compute the bytes output by each write operation, you could simply check the length of the file after it's closed using new File("t.dat").length()
.
If you wanted to figure it out without checking the length directly, an int
takes up 4 bytes, and something written with writeUTF
takes up 2 bytes to represented the encoded length of the string, plus the space the string itself takes, which in this case is another 4 bytes -- in UTF-8, each of the characters in "5678" requires 1 byte.
So that's 4 + 2 + 4, or 10 bytes.
Upvotes: 2