permaculture playing cards*
The moose likes I/O and Streams and the fly likes Regarding File Copy and make data & time same as source file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Regarding File Copy and make data & time same as source file" Watch "Regarding File Copy and make data & time same as source file" New topic
Author

Regarding File Copy and make data & time same as source file

Ankit Thakkar
Ranch Hand

Joined: Apr 29, 2005
Posts: 33

I am trying to copy file on network pc's.
and I want date & time of copied file same as source file.
I am setting file date & time using file class.

***********************************

File sfile=new File();
File dfile=new File();

dfile.setLastmodified(sfile.getLastmodified());

************************************

But sometime I am getting date & time of copied file different than the source file.

Plz help me to solve this problem.


- Ankit
Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
I tried this out. If you call setLastModified before actually creating the file, it is not setting it correctly. Where in if you call setLastModified after creating the file, it is working. I tried this in Jdk 1.5 in Windows XP.
Ankit Thakkar
Ranch Hand

Joined: Apr 29, 2005
Posts: 33

Thanks Santosh
U r right,
But I am setting file time for network pc's
so if that file has been opened by another user then
I am not able to set the file time
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
If other users are reading the file before you're done creating it, you need to prevent them from doing that. The usual way to achieve this is to create the file somewhere else, preferably in a direcory other users do not have access to, or which they don't know about or which they know not to look into. File.createTemporaryFile() is one way to do this. Once you have completely copied the file (closing the streams) and set its last modified time, then you use File.renameTo() to move the file to a new directory and new name. (Your temporary directory will probably need to be apart of the same filesystem as the intended target directory for this to work.) When the file appears in the new directory, it is ready to be seen by other users.
[ January 07, 2006: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Ankit Thakkar
Ranch Hand

Joined: Apr 29, 2005
Posts: 33

Thanx JIM
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Regarding File Copy and make data & time same as source file