We help IT Professionals succeed at work.

Modify file content without affecting created timestamp

Daniel Södling

I need to modify the content of a fairly simple json file without recreating the file each time. The reason is that I would like to keep the original time the file was first created. I've tried various approaches using both streams and channels but I cannot get it to work. The created file attribute is always updated.

This must be possible (right?) but apparently I am missing something.
Any thoughts?

Best regards
Daniel Södling
Watch Question

What code are you using to modify the file contents?

Normally when we're writing files we just do it sequentially - which means you rewrite the file and really are deleting and creating a new one in its place.

If you want to modify the internals of the file without re-writing it, I think you may need to treat it as a RandomAccessFile:
which allows you to move around inside a file, rewrite portions of it and set its length.  It would require more care, should be possible to do what you need.  This should only change the "lastModified" time for the file, not when it was created.

I would simply read the attribute before any changes/before save,
then write it back after my modifications.
Nice George - I was aware you could change over file attributes but didn't realize creation time was one of them (interesting that you can have the file system essentially lie to you!).

Anyway the steps are here Daniel:

Top Expert 2016

Another possible approach is to move the file to a filesystem that doesn't support that attribute, such as Linux/Unix ext2/ext3



thanks a bunch for your replies. I think the problem I'm seeing is related to the file system. I'm on Mac OS X and I'm guessing that it as a UNIX based system does not support the creation timestamp. At least not the retrieval of it from Java as I keep getting the last modified timestamp each time I ask for the creation time.

Thanks again to all of you
Top Expert 2016

I think the problem I'm seeing is related to the file system. I'm on Mac OS X
HFS doesn't support file created. HFSPlus does in theory