java - “文件”类对象大小?
问题描述
我想知道一个File
类的对象是否将整个文件加载到主内存中。我想用一大一小两个文件来制作文件类的对象,然后比较这两个对象的大小。但显然,没有直接的方法来确定Java
.
解决方案
File
object is just a plain object with a reference to a file path. The referenced file may or may not actually exist in the file system. File object does not hold content of the file.
When you read a file using InputStream
(e.g. FileInputStream
) or Reader
(e.g. FileReader
) in conjunction with a Buffer (e.g. BufferedReader
), you start reading the actual file content. Now, it is up to you whether you want to keep the whole file content data in the memory or process chunk by chunk and discard it. So, whether or not full file content is loaded into memory depends on your application.
In order to know the file size upfront in bytes, you may do: file.length()
In order to know the file content size after reading, while reading the file store content into byte array
(byte[]
) and measure the length of the array using mybytes.length
.
Update
You have mentioned in the comment that you want to find out the size of File
object. File
object is just another usual object. Still, if you want to measure the size use java.lang.instrument.Instrumentation#getObjectSize()
Please refer to this article How to use the Java Instrumentation API to understand how to determine object size using java.lang.instrument
classes.
推荐阅读
- reactjs - 如何在 React Native Redux - Reducer 中实现以下内容
- html - 无法根据中心原点旋转齿轮
- virtual-reality - 是否可以使用 openvr 为 HMD 中的显示添加失真?
- c# - ASP .NET Core:如何缓存依赖于用户输入的数据?
- jquery - kendo ui jquery工具栏按钮徽章不起作用
- c++ - 按其子元素之一对 QJsonArray 进行排序
- computer-vision - RetinaNet 中的焦点损失
- mongodb - 如何在 MongoDB 中将集合导出到 TSV
- axis2 - 无法使用 jibx 解析解组 xsi:nil=true
- github - 如何使用 ssh_rsa 密钥对创建从 jupyter notebook 到 github 的永久登录