A file on your machine is associated with a File
object in Java. This allows the “stream of bytes” to have behaviors (methods) that can be useful for developers.
If I had just a stream of bytes, I can do little more than read the stream and do something with the data of the file. However, with a File
object, I can do a lot more, including (but certainly not limited to):
- Read metadata about the file (such as
lastModified
) compareTo
anotherFile
or see if two of them areequal
getAbsolutePath
of the file for reference
You can read about the functionality of File
here: https://docs.oracle.com/javase/7/docs/api/java/io/File.html
Finally, treating a file as an object falls in line with object-oriented design. Knowing that a file has a state and behaviors allows it to interact with other objects in the program to achieve a task.
Java attempts to be a purely object-oriented language, and so just about every concept can be in some way formed as an object.
4
solved why is a file associated with an object? [closed]