I need to compare the last modified timestamps from two places. If they do not match, I need to copy the file from first place to second, and also set the changed timestamp of the second from the first.
I am trying to do this using File.lastModified in java. But I get different File.lastModified values ββfor the same file at different times, even if they do not change. Please note that I am trying to do this on Linux.
Can someone point out what is going wrong?
thanks
Copy code:
private static boolean copyFiles(File src, File destFolder, String[] existingROOTNames) { final String[] fileNames = src.list(); boolean changeRootContext = false; File srcFile = null; File destFile = null; List rootFileList = Arrays.asList(existingROOTNames); int rootFileIndex = -1; long srcFileTime; long destFileTime; for (int index = 0; index < fileNames.length; index++) { srcFile = new File(src, fileNames[index]); destFile = new File(destFolder.getPath(),fileNames[index]); if (srcFile.isFile()) { if (log.isEnabled(DEBUG)) { log.debug("copy file : " + srcFile); } srcFileTime = srcFile.lastModified(); destFileTime = destFile.lastModified(); if(hasFileChanged(srcFileTime,destFileTime)){ changeRootContext = true; if (log.isEnabled(XDEBUG)) { log.debug(XDEBUG,"changing flag to true for : " + srcFile); log.debug(XDEBUG,"changing flag srcFile.lastModified() : " + srcFileTime); log.debug(XDEBUG,"changing flag destFile.lastModified() : " + destFileTime); } } try { FileUtil.fastChannelCopy(srcFile.getPath(), destFolder.getPath()); log.debug("changing flag while modifying destFile.lastModified() : " + destFile.setLastModified(srcFileTime)); log.debug("changing flag after modifying destFile.lastModified() : " + destFile.lastModified()); rootFileIndex = rootFileList.indexOf(fileNames[index]); if(rootFileIndex!=-1){ existingROOTNames[rootFileIndex]=null; } } catch (IOException e) { log.debug("unable to copy source file : " + fileNames[index], e); } } } return changeRootContext; } private static boolean hasFileChanged(long srcFileTime, long destFileTime){ return Math.abs(srcFileTime-destFileTime) > 1000l; }
Ishita
source share