下面的方法返回文件大小为 2。由于它很长,我假设 java 计算的文件大小是 2*64 位。但实际上我保存了 32 位 int + 16 位 char = 48 位。Java 为什么要进行这种转换?此外,无论是 char 还是 int,Java 是否都会将所有内容都隐式存储在文件中?如何获得 48 位的准确大小?
public static void main(String[] args)
{
File f = new File("C:/sam.txt");
int a= 42;
char c= '.';
try {
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
PrintWriter pw = new PrintWriter(f);
pw.write(a);
pw.write(c);
pw.close();
System.out.println("file size:"+f.length());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}