Java File class serialized and sent through network doesn’t have all information about file

Tags: ,



I want to send java.io.File object through network using serialization. When i serialize some File object, send it and deserialize it on other side, it doesn’t contain all information about file. For example i can get path where file was (on server side where i serialized it), but i can’t get file size, file modification date, is file directory or no and some other values. What can be problem? Is there any way to load all that info before serializing File object? I’m trying to do that without creating my custom class for File representation.

Answer

All serialisation does is transfer the non-transient instance (not static) fields of an object. If you look at the File class, that includes the path — and nothing else.

Attributes of the file are checked against the actual filesystem when a method like lastModified() is called. This is good because you want to know what these attributes are now, not at the point at which you created a File object.

Create your own class as a snapshot of the file state.



Source: stackoverflow